Brute Force Approach:
To identify and eliminate duplicates simultaneously, we may generate a new array that is the exact length of the original array and compare each element to its neighbor. In the case of a successful comparison, we would transfer one instance of the matching value from the original to the new array.
Solution Steps:
- Return the array by checking if the array's length is
0
or1
. - Declare a variable
temp
to store the single instance of each element. - The input array will be scanned and copied a single instance of each element from
arr
totemp
. - The variable
p
will be tracking the count of the single instance of each element. - We will copy each element's single instance from
temp
toarr
. - And last, we will return the length of the array and the resultant
arr
by eliminating the duplicated element.

Here's to reference code for a better understanding!
1import java.util.Arrays;
2import java.util.List;
3import java.util.ArrayList;
4import java.util.Collections;
5
6class Main
7{
8 static int removeDuplicates(int arr[], int N)
9 {
10 if (N==0 || N==1)
11 return N;
12
13 int[] temp = new int[N];
14
15 int p = 0;
16 for (int i=0; i<N-1; i++)
17 if (arr[i] != arr[i+1])
18 temp[p++] = arr[i];
19
20 temp[p++] = arr[N-1];
21
22 for (int i=0; i<p; i++)
23 arr[i] = temp[i];
24
25 return p;
26 }
27
28 public static void main (String[] args)
29 {
30 int arr[] = {2,3,4,4,5,6,6,6,7,8,8};
31 int N = arr.length;
32 N = removeDuplicates(arr, N);
33
34 int[] newarr = new int[N];
35 for (int k=0; k<N; k++){
36 newarr[k]=arr[k];
37 }
38 System.out.print(Arrays.toString(newarr));
39 }
40}