-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates1.java
More file actions
45 lines (33 loc) · 1.1 KB
/
RemoveDuplicates1.java
File metadata and controls
45 lines (33 loc) · 1.1 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
package DSA;
import java.util.Arrays;
import java.util.Scanner;
public class RemoveDuplicates1 {
public static void main(String[] args){
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:arr){
System.out.print(i + " ");
}
int i=0,j=0;
for(j=1;j<arr.length;j++){
if(arr[i]!=arr[j]){
arr[i+1]=arr[j];
i++;
}
}
System.out.print("\nArray after removing duplicates : ");
for(int k=0;k<=i;k++){
System.out.print(arr[k] + " ");
}
System.out.println("\nNumber of unique elements are : " + (i+1));
}
}