-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemoveDuplicates.java
40 lines (36 loc) · 1.11 KB
/
RemoveDuplicates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class RemoveDuplicates{
//remove Duplicates program...
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
// Changing original array
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
System.out.println("Original Array Elements...");
for (int i=0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("After removing Duplicate Elements...");
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++) {
System.out.print(arr[i] + " ");
}
}
}