-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathremoveDuplicates.java
More file actions
47 lines (29 loc) · 844 Bytes
/
removeDuplicates.java
File metadata and controls
47 lines (29 loc) · 844 Bytes
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
41
42
43
44
45
46
package arrays;
import java.util.Arrays;
public class Duplicate {
public static void main(String[] args){
int[] arrays = {1,1,2,3,3,4,5,6};
System.out.println(Arrays.toString(removeDuplicates(arrays)));
}
private static int removeDuplicates(int a[], int n) {
if (n == 0 || n == 1) {
return n;
}
int j = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] != a[i + 1]) {
a[j++] = a[i];
}
}
a[j++] = a[n - 1];
return j;
}
public static int[] removeDuplicates(int[] arr) {
int result = removeDuplicates(arr, arr.length);
int[] newArray = new int[result];
for (int i = 0; i < result; i++) {
newArray[i] = arr[i];
}
return newArray;
}
}