forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDup.java
More file actions
41 lines (40 loc) · 933 Bytes
/
removeDup.java
File metadata and controls
41 lines (40 loc) · 933 Bytes
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
import java.util.*;
class removeDup
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
int temp[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int j=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
temp[j++]=arr[i];
}
}
temp[j++]=arr[n-1];
for(int i=0;i<j;i++)
{
System.out.print(temp[i]+" ");
}
System.out.println();
System.out.println("hash set method");
// 2nd mwthod using hashser
HashSet<Integer> h=new HashSet<>();
for(int i=0;i<n;i++)
{
h.add(arr[i]);
}
for(int x:h)
{
System.out.print(x+" ");
}
}
}