大二算法题、有点不会思路 有很多c++的代码 但没有java的,所以想问问大家

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

public class Test {
/*
2
11
1 2 18 3 3 19 2 3 6 5 4
6
1 2 3 4 5 6

 */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t-- > 0) {
            int n = scanner.nextInt();
            Set<Integer> set = new LinkedHashSet<>();
            for (int i = 0; i < n; i++) {
                set.add(scanner.nextInt());
            }
            for (int v : set) {
                System.out.print(v + " ");
            }
            System.out.println();
        }
        scanner.close();
    }
}

输出示例:

img



import java.util.HashMap;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    for(int i = 0;i < T;i++){
      int N = sc.nextInt(),n = 0;
      int arr[] = new int[N];
      HashMap<Integer,Integer> map = new HashMap<>();
      for(int j = 0;j < N;j++){
        int t = sc.nextInt();
        if(!map.containsKey(t)){
          map.put(t, 1);
          arr[n++] = t;
        }
      }
      for(int j = 0;j < n;j++){
        System.out.print(arr[j] + " ");
      }
      System.out.println();
    }
  }
}

java有很多Api可以直接调用了,Set自身带去重性,直接把这些数据一个一个放Set里就可以去重了。

算法方面可以使用暴力法和排序法。
暴力法就是一个一个放进去,放的时候,先一个一个检查已经放进去的有没有相同的,不相同就放,相同就下一个。
排序法就直接把这些东西进行排序,然后有一样的就成连续的了,再剔除就可以。也可以在排序的时候判断一下等于的情况,等于就直接丢了。
如果有字符串的这种可以hash法。