How to efficiently remove duplicates from an array without using Set How to efficiently remove duplicates from an array without using Set arrays arrays

How to efficiently remove duplicates from an array without using Set


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());}


If you are allowed to use Java 8 streams:

Arrays.stream(arr).distinct().toArray();


Note: I am assuming the array is sorted.

Code:

int[] input = new int[]{1, 1, 3, 7, 7, 8, 9, 9, 9, 10};int current = input[0];boolean found = false;for (int i = 0; i < input.length; i++) {    if (current == input[i] && !found) {        found = true;    } else if (current != input[i]) {        System.out.print(" " + current);        current = input[i];        found = false;    }}System.out.print(" " + current);

output:

  1 3 7 8 9 10