-
Notifications
You must be signed in to change notification settings - Fork 17
/
FirstRepeatingElement.java
42 lines (40 loc) · 1.27 KB
/
FirstRepeatingElement.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
package SummerTrainingGFG.Hashing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
/**
* @author Vishal Singh
*/
public class FirstRepeatingElement {
static int firstRepeatingElement(int[] arr,int n){
LinkedHashMap<Integer,Integer> map= new LinkedHashMap<>();
for (int i = 0; i <n; i++) {
if (map.containsKey(arr[i])){
map.put(arr[i],map.get(arr[i])+1);
}
else {
map.put(arr[i],1);
}
}
for (int i = 0; i < n; i++) {
if ((map.get(arr[i])>1)){
return (i+1);
}
}
return -1;
}
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = Integer.parseInt(br.readLine());
while (testCase-->0){
int n = Integer.parseInt(br.readLine());
String[] curr = br.readLine().split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(curr[i]);
}
System.out.println(firstRepeatingElement(arr,arr.length));
}
}
}