去重(底层数据结构哈希表)

    import java.util.HashSet;
    import java.util.Scanner;
    import java.util.Set;

    public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] ant = new int[n];
    for (int i = 0; i < n; i++) {
        ant[i] = sc.nextInt();
    }
    Set<Integer> set = new HashSet<>();
    for (int i = 0; i < ant.length; i++) {
        if (!set.contains(ant[i])) {
            System.out.print(ant[i]);
            if (i != n - 1) {
                System.out.print(" ");
            }
            set.add(ant[i]);
        }
    }
  }
}
全部评论

相关推荐

点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
06-19 13:40
武汉大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务