-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
50 lines (36 loc) · 1.25 KB
/
RemoveDuplicates.java
File metadata and controls
50 lines (36 loc) · 1.25 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
47
48
49
50
package DSA;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class RemoveDuplicates {
public static void main(String[] args){
//REMOVE DUPLICATES FROM SORTED ARRAY
//NOT OPTIMAL SOLUTION
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array : ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("\nEnter the array elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("Element %d : ", i);
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
System.out.print("\nArray before removing duplicates : ");
for (int i=0;i<arr.length;i++) {
System.out.print(arr[i] + " ");
}
HashSet<Integer> unique = new HashSet<>();
for(int i : arr){
unique.add(arr[i]);
}
int count=1;
System.out.println("\nArray after removing duplicates : ");
for(int i : unique){
System.out.print(i + " ");
count+=1;
}
System.out.println("\nThe number of unique elements are : " + count);
}
}