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.

5
  • Possible duplicate of Get unique values from arraylist in java Commented Apr 7, 2017 at 16:31
  • @Marc H. I edited my code, you can test that. Commented Apr 7, 2017 at 16:58
  • Please add more context. All you show for now is a loop that adds elements to a set. Then what? Commented Apr 7, 2017 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? Commented Apr 7, 2017 at 17:16
  • @Okapist the dupe is about ArrayList, not array. Commented Apr 7, 2017 at 17:17

3 Answers 3

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

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]

2 Comments

Thanks for your answer. Sadly I am always receiving the following warning : The type List is not generic; it cannot be parameterized with arguments <Integer>
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).
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]

1 Comment

This will not work if there are odd number of duplicates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.