Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently remove duplicates from an array without using Set

I was asked to write my own implementation to remove duplicated values in an array. Here is what I have created. But after tests with 1,000,000 elements it took very long time to finish. Is there something that I can do to improve my algorithm or any bugs to remove ?

I need to write my own implementation - not to use Set, HashSet etc. Or any other tools such as iterators. Simply an array to remove duplicates.

public static int[] removeDuplicates(int[] arr) {      int end = arr.length;      for (int i = 0; i < end; i++) {         for (int j = i + 1; j < end; j++) {             if (arr[i] == arr[j]) {                                   int shiftLeft = j;                 for (int k = j+1; k < end; k++, shiftLeft++) {                     arr[shiftLeft] = arr[k];                 }                 end--;                 j--;             }         }     }      int[] whitelist = new int[end];     for(int i = 0; i < end; i++){         whitelist[i] = arr[i];     }     return whitelist; } 
like image 635
ashur Avatar asked Jul 31 '13 09:07

ashur


People also ask

How do you remove duplicate values from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.


2 Answers

If you are allowed to use Java 8 streams:

Arrays.stream(arr).distinct().toArray(); 
like image 41
Tomin Avatar answered Sep 23 '22 13:09

Tomin


you can take the help of Set collection

int end = arr.length; Set<Integer> set = new HashSet<Integer>();  for(int i = 0; i < end; i++){   set.add(arr[i]); } 

now if you will iterate through this set, it will contain only unique values. Iterating code is like this :

Iterator it = set.iterator(); while(it.hasNext()) {   System.out.println(it.next()); } 
like image 128
Android Killer Avatar answered Sep 25 '22 13:09

Android Killer