Skip to content

Commit de9b8c0

Browse files
solves remove duplicae from sorted array
1 parent 4239431 commit de9b8c0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| 13 | [Roman To Integer](https://leetcode.com/problems/roman-to-integer/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/RomanToInteger.java) |
1010
| 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LongestCommonPrefix.java) |
1111
| 20 | [ValidParentheses](https://leetcode.com/problems/valid-parentheses/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ValidParentheses.java) |
12-
| 21 | [Merge 2 Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | Easy | |
13-
| 2 | []() | Easy | |
12+
| 21 | [Merge 2 Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/Merge2SortedLists.java) |
13+
| 26 | [remove Duplicates From Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | Easy | |
1414
| 2 | []() | Easy | |
1515
| 2 | []() | Easy | |
1616
| 2 | []() | Easy | |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class RemoveDuplicatesFromSortedArray {
2+
public int removeDuplicates(int[] array) {
3+
if (array.length == 0) {
4+
return 0;
5+
}
6+
7+
int position = 1;
8+
for (int index = 1, previouslyEncountered = array[0]; index < array.length ; index++) {
9+
if (array[index] != previouslyEncountered) {
10+
array[position++] = array[index];
11+
previouslyEncountered = array[index];
12+
}
13+
}
14+
15+
return position;
16+
}
17+
}

0 commit comments

Comments
 (0)