-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveArrayDuplicates.java
48 lines (40 loc) · 1.06 KB
/
RemoveArrayDuplicates.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
41
42
43
44
45
46
47
48
package edu.buffalo.liveramp;
public class RemoveArrayDuplicates {
public int removeDuplicates(int[] nums) {
int c= 0;
int p =1;
if(nums.length<=1)
return nums.length;
for(int i = 0;i<nums.length -1 ;i++)
{
int curr = nums[i];
int next = nums[i+1];
if(curr==next)
{
if(i+2 < nums.length)
{
nums[i+1] = nums[i+2];
if(nums[i+2] != curr )
{
nums[p] = nums[i+2];
c++;
p= p+1;
}
}
else
c++;
}
else
{
if(i+1 ==nums.length -1)
c+=2;
else
{
c++;
p= p+1;
}
}
}
return c;
}
}