Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ed8f61

Browse files
26: Remove Duplicates from Sorted Array
1 parent afbab2f commit 2ed8f61

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ My solutions to LeetCode problems in Kotlin.
1111
| [8](https://leetcode.com/problems/string-to-integer-atoi/) | [String to Integer (atoi)](src/main/kotlin/com/schmoczer/leetcode/_0008/StringToInteger.kt) | Medium |
1212
| [20](https://leetcode.com/problems/valid-parentheses/) | [Valid Parentheses](src/main/kotlin/com/schmoczer/leetcode/_0020/ValidParentheses.kt) | Easy |
1313
| [27](https://leetcode.com/problems/remove-element/) | [Remove Element](src/main/kotlin/com/schmoczer/leetcode/_0027/README.md) | Easy |
14-
[42](https://leetcode.com/problems/trapping-rain-water/) | [Trapping Rain Water](src/main/kotlin/com/schmoczer/leetcode/_0042/TrappingRainWater.kt) | Hard |
14+
| [26](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Remove Duplicates from Sorted Array](src/main/kotlin/com/schmoczer/leetcode/_0026/RemoveDuplicatesFromSortedArray.kt) | Easy |
15+
| [42](https://leetcode.com/problems/trapping-rain-water/) | [Trapping Rain Water](src/main/kotlin/com/schmoczer/leetcode/_0042/TrappingRainWater.kt) | Hard |
1516
| [48](https://leetcode.com/problems/rotate-image/) | [Rotate Image](src/main/kotlin/com/schmoczer/leetcode/_0048/RotateImage.kt) | Medium |
1617
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
1718
| [54](https://leetcode.com/problems/spiral-matrix/) | [Spiral Matrix](src/main/kotlin/com/schmoczer/leetcode/_0054/SpiralMatrix.kt) | Medium |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Remove Duplicates from Sorted Array
2+
3+
Given an integer array `nums` sorted in non-decreasing order, remove the duplicates in-place such that each unique
4+
element appears only once. The relative order of the elements should be kept the same. Then return the number of unique
5+
elements in `nums`.
6+
7+
Consider the number of unique elements of `nums` to be `k`, to get accepted, you need to do the following things:
8+
9+
- Change the array `nums` such that the first `k` elements of `nums` contain the unique elements in the order they were
10+
present in `nums` initially. The remaining elements of `nums` are not important as well as the size of `nums`.
11+
- Return `k`.
12+
13+
Example 1:
14+
15+
> Input: nums = [1,1,2]
16+
>
17+
> Output: 2, nums = [1,2,_]
18+
19+
Example 2:
20+
21+
> Input: nums = [0,0,1,1,1,2,2,3,3,4]
22+
>
23+
> Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
24+
25+
Constraints:
26+
27+
- `1 <= nums.length <= 3 * 10^4`
28+
- `-100 <= nums[i] <= 100`
29+
- `nums` is sorted in non-decreasing order
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.schmoczer.leetcode._0026
2+
3+
class RemoveDuplicatesFromSortedArray {
4+
// Runtime 1ms Beats 100.00%
5+
fun removeDuplicates(nums: IntArray): Int {
6+
val copied = IntArray(nums.size)
7+
System.arraycopy(nums, 0, copied, 0, nums.size)
8+
var previousNumber: Int = -101
9+
var result = 0
10+
for (currentNumber in copied) {
11+
if (currentNumber != previousNumber) {
12+
nums[result] = currentNumber
13+
result++
14+
previousNumber = currentNumber
15+
}
16+
}
17+
return result
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.schmoczer.leetcode._0026
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.params.ParameterizedTest
6+
import org.junit.jupiter.params.provider.Arguments
7+
import org.junit.jupiter.params.provider.MethodSource
8+
9+
class RemoveDuplicatesFromSortedArrayTest {
10+
private companion object {
11+
@JvmStatic
12+
fun numbersAndOutput() = listOf(
13+
Arguments.of(intArrayOf(1, 1, 2), 2, intArrayOf(1, 2)),
14+
Arguments.of(intArrayOf(0, 0, 1, 1, 1, 2, 2, 3, 3, 4), 5, intArrayOf(0, 1, 2, 3, 4)),
15+
)
16+
}
17+
18+
private lateinit var sut: RemoveDuplicatesFromSortedArray
19+
20+
@BeforeEach
21+
fun setUp() {
22+
sut = RemoveDuplicatesFromSortedArray()
23+
}
24+
25+
@ParameterizedTest(name = "when removing duplicates from {0} then the first {1} elements are {2}")
26+
@MethodSource("numbersAndOutput")
27+
fun `returns the number of unique elements`(input: IntArray, expectedResult: Int, expectedNumbers: IntArray) {
28+
val result = sut.removeDuplicates(input)
29+
30+
assertEquals(expectedResult, result)
31+
for (i in 0 until result) {
32+
assertEquals(expectedNumbers[i], input[i])
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /