-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LC 26: Remove Duplicates from Sorted Array
- Loading branch information
1 parent
5ec5545
commit 0d6ce27
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/com/company/leetcode/numbers/RemoveDupesFromArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.company.leetcode.numbers; | ||
|
||
/** | ||
LC 26: Remove Duplicates from Sorted Array | ||
Given an integer array nums sorted in non-decreasing order, | ||
remove the duplicates in-place such that each unique element appears only once. | ||
The relative order of the elements should be kept the same. | ||
Then return the number of unique elements in nums. | ||
Consider the number of unique elements of nums to be k, | ||
to get accepted, you need to do the following things: | ||
Change the array nums such that the first k elements of nums contain the unique elements | ||
in the order they were present in nums initially. | ||
The remaining elements of nums are not important as well as the size of nums. | ||
Return k. | ||
*/ | ||
public class RemoveDupesFromArray { | ||
public int removeDuplicates(int[] nums) { | ||
int cur = 1; | ||
int next = 1; | ||
while (next<nums.length) { | ||
if (nums[next]==nums[cur-1]) next++; | ||
else { | ||
nums[cur] = nums[next]; | ||
cur++; | ||
} | ||
} | ||
return cur; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/com/company/leetcode/numbers/RemoveDupesFromArrayTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.company.leetcode.numbers; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class RemoveDupesFromArrayTest { | ||
public static Object[][] data() { | ||
return new Object[][]{ | ||
new Object[]{2, new int[]{1,1,2}}, | ||
new Object[]{5, new int[]{0,0,1,1,1,2,2,3,3,4}} | ||
}; | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("data") | ||
public void removeDuplicatesTest(int expected, int[] given) { | ||
assertEquals(expected, new RemoveDupesFromArray().removeDuplicates(given)); | ||
} | ||
|
||
} |