-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP28_RemoveDuplicatesSortedArray.java
More file actions
46 lines (37 loc) · 1.44 KB
/
Copy pathP28_RemoveDuplicatesSortedArray.java
File metadata and controls
46 lines (37 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package programs;
import java.util.Arrays;
/**
* ============================================================
* PROGRAM 28: Remove Duplicates from Sorted Array In-Place
* ============================================================
* Problem: WAP to remove duplicates from a sorted array in-place
* such that each element appears only once and returns the new length.
* - Input : [1, 1, 2, 2, 3, 4, 4, 5]
* - Output: Length = 5, Array prefix = [1, 2, 3, 4, 5]
* - Time: O(n), Space: O(1)
* ============================================================
*/
public class P28_RemoveDuplicatesSortedArray {
public static int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int writeIndex = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[i - 1]) {
nums[writeIndex] = nums[i];
writeIndex++;
}
}
return writeIndex;
}
public static void main(String[] args) {
int[] sorted = {1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 6};
System.out.println("Original Sorted Array : " + Arrays.toString(sorted));
int newLen = removeDuplicates(sorted);
System.out.println("Unique Element Count : " + newLen);
System.out.print("Deduped Array Prefix : [ ");
for (int i = 0; i < newLen; i++) {
System.out.print(sorted[i] + " ");
}
System.out.println("]");
}
}