-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDupInArray.java
More file actions
39 lines (28 loc) · 814 Bytes
/
Copy pathRemoveDupInArray.java
File metadata and controls
39 lines (28 loc) · 814 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
package Array;
class RemoveDupInArray {
/*This program just returns the index of the duplicate element found in the array
which is sorted or it can return the element thats it
O(n) Solution
*/
public static void main(String[] args) {
int[] a = {1,1,2,3,4,5,5,6,7};
int ans = removeDuplicates(a);
System.out.println("Duplicate Elment is at INDEX : "+ ans);
}
public static int removeDuplicates(int[] A) {
int len = A.length;
int i = 0;
if (len <= 1)
return len;
for (int j = 1; j < len; j++) {
//When the element r not same eg 2 3 them go ahead
if (A[j] != A[i]){
A[++i] = A[j]; //this moves the pointer ahead in both ie i and j
//System.out.println(A[j] + " "+ A[++i] + " ");
}
}
//return i + 1;
//to return the dup element
return A[i+1];
}
}