Skip to content

Commit ef6181e

Browse files
authored
Create Remove Duplicates from Sorted Array
1 parent 6d3efaf commit ef6181e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Remove Duplicates from Sorted Array

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
4+
if(nums.length==0){
5+
return 0;
6+
}
7+
else if(nums.length == 1){
8+
return 1;
9+
}
10+
int i = numberOf(nums);
11+
removeElements(nums);
12+
13+
14+
15+
return i;
16+
}
17+
18+
public int[] removeElements(int[] a){
19+
int j = 1;
20+
for(int i = 0; i<a.length-1; i++){
21+
if(a[i] != a[i+1]){
22+
a[j] = a[i+1];
23+
j++;
24+
}
25+
}
26+
27+
return a;
28+
}
29+
30+
public int numberOf(int[] a){
31+
int count = 1;
32+
for(int i = 0; i<a.length-1; i++){
33+
if(a[i] != a[i+1]){
34+
count++;
35+
}
36+
}
37+
38+
return count;
39+
}
40+
}

0 commit comments

Comments
 (0)