Skip to content

Commit f4d1c9b

Browse files
Remove duplicated from sorted array
1 parent 382a1f1 commit f4d1c9b

3 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package array.remove_duplicates_from_sorted_array;
2+
3+
/**
4+
* Provides a method to remove duplicate elements from a sorted integer array
5+
* in-place such that each unique element appears only once.
6+
*
7+
* <p>The method preserves the relative order of elements and returns the
8+
* number of unique elements. The first {@code k} positions of the array
9+
* will contain the unique elements in sorted order.</p>
10+
*
11+
* <p>Constraints:
12+
* <ul>
13+
* <li>The input array must be sorted in non-decreasing order.</li>
14+
* <li>The operation is performed in-place with O(1) extra space.</li>
15+
* </ul>
16+
* </p>
17+
*/
18+
public class RemoveDuplicates {
19+
20+
/**
21+
* Removes duplicates from a sorted integer array in-place.
22+
*
23+
* <p>After execution:
24+
* <ul>
25+
* <li>The first {@code k} elements of {@code nums} contain unique values.</li>
26+
* <li>The remaining elements beyond index {@code k - 1} are unspecified.</li>
27+
* </ul>
28+
* </p>
29+
*
30+
* @param nums a sorted array of integers in non-decreasing order
31+
* @return the number of unique elements in the array
32+
* @throws NullPointerException if {@code nums} is {@code null}
33+
*/
34+
public int removeDuplicates(int[] nums) {
35+
if (nums.length == 0 || nums.length == 1) {
36+
return nums.length;
37+
}
38+
39+
int idx = 0;
40+
for (int i = 1; i < nums.length; i++) {
41+
if (nums[i] != nums[idx]) {
42+
idx++;
43+
nums[idx] = nums[i];
44+
}
45+
}
46+
return ++idx;
47+
}
48+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Contains algorithms related to array manipulation.
3+
*
4+
* <p>This package focuses on in-place array operations that optimize
5+
* space complexity. The primary problem addressed here is removing
6+
* duplicate elements from a sorted array while maintaining order.</p>
7+
*
8+
* <p>Example use cases include:
9+
* <ul>
10+
* <li>Data normalization</li>
11+
* <li>Preprocessing sorted datasets</li>
12+
* <li>Optimizing memory usage</li>
13+
* </ul>
14+
* </p>
15+
*/
16+
package array.remove_duplicates_from_sorted_array;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package array.remove_duplicates_from_sorted_array;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
/**
9+
* Unit tests for {@link RemoveDuplicates}.
10+
*/
11+
class RemoveDuplicatesTest {
12+
13+
private final RemoveDuplicates solution = new RemoveDuplicates();
14+
15+
@Test
16+
void testArrayWithDuplicates() {
17+
int[] nums = {1, 1, 2, 2, 3};
18+
int k = solution.removeDuplicates(nums);
19+
20+
assertEquals(3, k);
21+
assertArrayEquals(new int[]{1, 2, 3}, copyFirstK(nums, k));
22+
}
23+
24+
@Test
25+
void testArrayWithoutDuplicates() {
26+
int[] nums = {1, 2, 3, 4};
27+
int k = solution.removeDuplicates(nums);
28+
29+
assertEquals(4, k);
30+
assertArrayEquals(new int[]{1, 2, 3, 4}, copyFirstK(nums, k));
31+
}
32+
33+
@Test
34+
void testSingleElementArray() {
35+
int[] nums = {5};
36+
int k = solution.removeDuplicates(nums);
37+
38+
assertEquals(1, k);
39+
assertArrayEquals(new int[]{5}, copyFirstK(nums, k));
40+
}
41+
42+
@Test
43+
void testEmptyArray() {
44+
int[] nums = {};
45+
int k = solution.removeDuplicates(nums);
46+
47+
assertEquals(0, k);
48+
}
49+
50+
@Test
51+
void testAllElementsSame() {
52+
int[] nums = {2, 2, 2, 2};
53+
int k = solution.removeDuplicates(nums);
54+
55+
assertEquals(1, k);
56+
assertArrayEquals(new int[]{2}, copyFirstK(nums, k));
57+
}
58+
59+
/**
60+
* Utility method to copy the first k elements from an array.
61+
*/
62+
private int[] copyFirstK(int[] nums, int k) {
63+
int[] result = new int[k];
64+
System.arraycopy(nums, 0, result, 0, k);
65+
return result;
66+
}
67+
}

0 commit comments

Comments
 (0)