1

I want to remove every duplicate from an array in Java and store the remaining integers in the same array.

E.g.: int[] = { 5,5,5,3,4,4,2,2,1}; ==> int[] = {3,1};

So far I have tried using:

Set<Integer> set = new HashSet<Integer>();

for (int i = 0; i < array.length; i++) {
  set.add(array[i]);
}

It appears though, that this only removes one of the duplicates and not both.

Any help would be appreciated.

Marc H.
  • 11
  • 3
  • Possible duplicate of [Get unique values from arraylist in java](http://stackoverflow.com/questions/13429119/get-unique-values-from-arraylist-in-java) – Okapist Apr 07 '17 at 16:31
  • @Marc H. I edited my code, you can test that. – Alekhya Apr 07 '17 at 16:58
  • Please add more context. All you show for now is a loop that adds elements to a set. Then what? –  Apr 07 '17 at 17:16
  • 1
    One possible problem is that Java arrays are not resizeable. What do you do with array elements that used to contain the duplicates? –  Apr 07 '17 at 17:16
  • @Okapist the dupe is about ArrayList, not array. –  Apr 07 '17 at 17:17

3 Answers3

0

This should work :

int[] array = { 5,5, 5, 3, 4, 4, 2, 2, 1};

 Arrays.sort(array);
 Set<Integer> set = new HashSet<Integer>();

 Set<Integer> duplicateSet = new HashSet<Integer>();

 for (int i = 0; i < array.length; i++) {

     if(!set.add(array[i])){
         duplicateSet.add(array[i]);
     }
 }


 System.out.println(duplicateSet.toString());
 set.removeAll(duplicateSet);
 System.out.println(set.toString());

output :

[1,3]
Vasu
  • 21,832
  • 11
  • 51
  • 67
Alekhya
  • 272
  • 1
  • 7
0

You can use a HashMap to maintain the count of each element and remove those elements that have count greater than 1.

public int[] removeDuplicates(int[] arr) {
   Map<Integer, Integer> countMap = new LinkedHashMap<>(); // To maintain the order
   for (int n : arr) {
      Integer count = countMap.get(n);
      if (count == null) {
         count = 0;
      }
      count++;
      countMap.put(n, count);
   }

   for(Iterator<Map.Entry<String, String>> it = countMap.entrySet().iterator(); it.hasNext(); ) {
      Map.Entry<String, String> entry = it.next();
      if (entry.getValue() > 1) {
         it.remove();
      }
   }

   return new ArrayList<>(countMap.keySet()).toArray(new int[0]);
}
prasanth
  • 3,502
  • 4
  • 28
  • 42
0

You don't even a set, it is very simple, convert the array to a List and then use Collections.frequency to check for duplicates as shown below:

    Integer[] array = { 5,5,5,3,4,4,2,2,1};
    List<Integer> listInputs = Arrays.asList(array);
    List<Integer> listOutputs = new ArrayList<>();
    for(int value : listInputs) {
         if(Collections.frequency(listInputs, value) ==1) {
             listOutputs.add(value);
         }
    }
    System.out.println(listOutputs);

OUTPUT: [3, 1]

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Thanks for your answer. Sadly I am always receiving the following warning : The type List is not generic; it cannot be parameterized with arguments – Marc H. Apr 07 '17 at 16:58
  • This is a nice idea. But `Collections.frequency()` has a runtime of `O(n)` which is bad. This code will have a total run time of `O(n^2)`. – prasanth Apr 07 '17 at 22:42