-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicates in Array.java
More file actions
44 lines (40 loc) · 862 Bytes
/
Duplicates in Array.java
File metadata and controls
44 lines (40 loc) · 862 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
42
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
//your code here
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] arr= new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
duplicate(arr);
}
static void duplicate(int[] arr)
{
ArrayList<Integer> list= new ArrayList<>();
int n=arr.length;
int repeat=-1;
Arrays.sort(arr);
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1]&&arr[i]!=repeat&&arr[i]!=-1)
{
repeat=arr[i+1];
arr[i+1]=-1;
list.add(arr[i]);
}
}
if(list.size()==0)
System.out.print("0");
else{
for(int i=0;i<list.size();i++)
System.out.print(list.get(i)+" ");
}
}
}