-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
45 lines (34 loc) · 1.14 KB
/
RemoveDuplicates.java
File metadata and controls
45 lines (34 loc) · 1.14 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
import java.util.Scanner;
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int j = 1; // pointer for next unique element
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[i - 1]) {
nums[j] = nums[i];
j++;
}
}
return j; // number of unique elements
}
}
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.println("Enter " + n + " elements (sorted):");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
Solution sol = new Solution();
int newLength = sol.removeDuplicates(nums);
System.out.println("Array after removing duplicates:");
for (int i = 0; i < newLength; i++) {
System.out.print(nums[i] + " ");
}
System.out.println("\nNumber of unique elements: " + newLength);
sc.close();
}
}