-
Notifications
You must be signed in to change notification settings - Fork 0
/
Findduplicatesinanarray.java
62 lines (58 loc) · 1.62 KB
/
Findduplicatesinanarray.java
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
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
/*Question: Find duplicate in an array
Link: https://practice.geeksforgeeks.org/problems/find-duplicates-in-an-array/1
Input:
N = 4
a[] = {0,3,1,2}
Output:
-1
Explanation:
There is no repeating element in the array. Therefore output is -1.
*/
public class Findduplicatesinanarray {
public static ArrayList<Integer> duplicates(int arr[], int n) {
/*
* This is another method using array instead of hashmap
* having TC:O(2n) and SC:O(n)
* int[] temp=new int[n];
* for(int i=0;i<n;i++){
* temp[arr[i]]++;
* }
* ArrayList<Integer> al=new ArrayList<>();
* for(int i=0;i<temp.length;i++){
* if(temp[i]>1){
* al.add(i);
* }
* }
* if(al.isEmpty())
* al.add(-1);
* return al;
*/
HashMap<Integer, Integer> h1 = new HashMap<>();
ArrayList<Integer> al = new ArrayList<>();
for (int i : arr) {
if (h1.containsKey(i)) {
h1.put(i, h1.get(i) + 1);
} else {
h1.put(i, 1);
}
}
int count = 0;
for (Map.Entry<Integer, Integer> map : h1.entrySet()) {
if (map.getValue() > 1) {
al.add(map.getKey());
count = 1;
}
}
if (count == 0) {
al.add(-1);
}
Collections.sort(al);
return al;
}
public static void main(String[] args) {
int[] arr = { 2, 0, 3, 3, 1 };
ArrayList<Integer> a1 = duplicates(arr, arr.length);
System.out.println(a1);
}
}