-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2.py
30 lines (23 loc) · 1023 Bytes
/
Q2.py
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
# https://practice.geeksforgeeks.org/problems/first-repeating-element4018/1/?category[]=Arrays&category[]=Arrays&difficulty[]=0&page=2&query=category[]Arraysdifficulty[]0page2category[]Arrays#
class Solution:
#Function to return the position of the first repeating element.
def firstRepeated(self,arr):
dictionary = {num: arr.count(num) for num in arr}
if all(frequency == 1 for frequency in dictionary.values()):
return -1
return min([arr.index(key) + 1 for key in dictionary.keys() if dictionary[key] > 1])
if __name__=='__main__':
t=int(input())
for _ in range(t):
arr=[int(x) for x in input().strip().split()]
ob = Solution()
print(ob.firstRepeated(arr))
# METHOD 2
# if list(set(arr)) == arr:
# return -1
# else:
# minimum = n
# for num in set(arr):
# if arr.count(num) > 1 and arr.index(num) + 1 < minimum:
# minimum = arr.index(num) + 1
# return minimum