Compare elements in an array for duplicates

后端 未结 8 773
轻奢々
轻奢々 2020-12-21 00:34

I am trying to generate a 5 digit int array in Java and am having trouble on where to start. None of the numbers in the array can be duplicates. I can generate

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 00:59

    You can get rid of the duplicates by converting the array into a TreeSet (that will also sort them):

    int numbers[] { 4 5 7 6 5 7 5 89 847 7 94 093 02 10 11 10 11 };
    TreeSet set new TreeSet(Arrays.asList(numbers));
    for (int no : set)
      System.out.println(no);
    

提交回复
热议问题