Given an array of N integers. Find the first element that occurs at least K number of times.
 
Example 1:
Input :
N = 7, K = 2
A[] = {1, 7, 4, 3, 4, 8, 7}
Output :
4
Explanation:
Both 7 and 4 occur 2 times. 
But 4 is first that occurs 2 times
As at index = 4, 4 has occurred 
atleast 2 times whereas at index = 6,
7 has occurred atleast 2 times.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function firstElementKTime() which takes the array A[], its size N, and an integer K as inputs and returns the required answer. If the answer is not present in the array, return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{4}
1 <= K <= 100
1<= A[i] <= 200

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3





class Solution:

    def firstElementKTime(self,  a, n, k):

        # code here

    



Solutions:
class Solution:

	def firstElementKTime(self, a, n, k):
		d = {}
		for i in a:
			if i in d.keys():
				d[i] += 1
			else:
				d[i] = 1
			if d[i] == k:
				return i
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		mydict = {}
		for i in a:
			if i in mydict:
				mydict[i] += 1
			else:
				mydict[i] = 1
			if mydict[i] == k:
				return i
		return -1


from collections import Counter

class Solution:

	def firstElementKTime(self, a, n, k):
		count = {}
		for i in range(0, n):
			if a[i] in count.keys():
				count[a[i]] += 1
				if count[a[i]] == k or count[a[i]] > k:
					return a[i]
					break
				i + 1
			else:
				count[a[i]] = 1
				if count[a[i]] == k:
					return a[i]
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		d = dict.fromkeys(a, 0)
		for i in a:
			d[i] += 1
			if d[i] == k:
				return i
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		elements = {}
		if n == 0:
			return -1
		if k == 1:
			return a[0]
		for i in a:
			if i in elements:
				elements[i] += 1
				if elements[i] == k:
					return i
			else:
				elements[i] = 1
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		d = {}
		for item in a:
			if item not in d:
				d[item] = 1
			else:
				d[item] += 1
			if d[item] == k:
				return item
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		a1 = max(a)
		vis = [0] * (a1 + 1)
		for i in a:
			vis[i] += 1
			if vis[i] == k:
				return i
				break
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		d = {}
		for num in a:
			if num not in d:
				d[num] = 0
			d[num] += 1
			if d[num] == k:
				return num
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		d = dict()
		result = -1
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			if d[i] == k:
				result = i
				break
		return result


class Solution:

	def firstElementKTime(self, a, n, k):
		count = {}
		for i in range(0, n):
			if a[i] in count.keys():
				count[a[i]] += 1
				if count[a[i]] >= k:
					return a[i]
			else:
				count[a[i]] = 1
				if count[a[i]] >= k:
					return a[i]
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		diction = {}
		for num in a:
			if num in diction:
				diction[num] += 1
				if diction[num] == k:
					return num
			else:
				diction[num] = 1
				if diction[num] == k:
					return num
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		from collections import defaultdict
		dic = defaultdict(int)
		for i in a:
			dic[i] += 1
			if dic[i] == k:
				return i
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		d = {}
		for num in a:
			if num in d:
				d[num] = d[num] + 1
			else:
				d[num] = 1
			for (num, f) in d.items():
				if f == k:
					return num
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		hashtable = {}
		for i in range(n):
			if a[i] not in hashtable.keys():
				hashtable[a[i]] = 1
			else:
				hashtable[a[i]] += 1
			if hashtable[a[i]] == k:
				return a[i]
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		b = {}
		for i in range(n):
			if a[i] in b:
				b[a[i]] += 1
			else:
				b[a[i]] = 1
			if b[a[i]] == k:
				return a[i]
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		counts = {}
		first_occurrence = {}
		for i in range(n):
			num = a[i]
			if num in counts:
				counts[num] += 1
			else:
				counts[num] = 1
			if counts[num] == k:
				first_occurrence[num] = i
		min_index = float('inf')
		result = -1
		for (num, index) in first_occurrence.items():
			if index < min_index:
				min_index = index
				result = num
		return result


class Solution:

	def firstElementKTime(self, A, N, K):
		mydict = {}
		for i in A:
			if i in mydict:
				mydict[i] += 1
			else:
				mydict[i] = 1
			if mydict[i] == K:
				return i
		return -1


class Solution:

	def firstElementKTime(self, arr, n, k):
		a = {}
		for i in arr:
			if i not in a:
				a[i] = 1
			else:
				a[i] += 1
			if a[i] == k:
				return i
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		dict = {}
		count = 0
		for i in a:
			dict[i] = 0
		for i in a:
			dict[i] += 1
			if dict[i] >= k:
				count += 1
				return i
		if count == 0:
			return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		l = {}
		for i in range(n):
			if a[i] in l:
				l[a[i]] += 1
			elif a[i] not in l:
				l[a[i]] = 1
			if l[a[i]] == k:
				return a[i]
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		freq = {}
		count = []
		for num in a:
			if num in freq:
				freq[num] += 1
				if freq[num] >= k:
					count.append(num)
			else:
				freq[num] = 1
				if freq[num] >= k:
					count.append(num)
		if len(count) > 0:
			return count[0]
		else:
			return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		counts = {}
		for (i, num) in enumerate(a):
			counts[num] = counts.get(num, 0) + 1
			if counts[num] == k:
				return num
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		new = dict()
		if k == 1:
			return a[0]
		for i in a:
			if i not in new.keys():
				new[i] = 1
			else:
				new[i] += 1
				if new[i] == k:
					return i
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		countdict = {}
		for i in a:
			if i not in countdict:
				countdict[i] = 1
			elif i in countdict:
				countdict[i] += 1
			if countdict[i] == k:
				return i
				break
		else:
			return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		d = {}
		for i in a:
			d[i] = d.get(i, 0) + 1
			if d[i] == k:
				return i
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		counter = dict.fromkeys(a, 0)
		for item in a:
			counter[item] += 1
			if counter[item] >= k:
				return item
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		y = {}
		for i in range(len(a)):
			if a[i] not in y:
				y[a[i]] = 1
			else:
				y[a[i]] += 1
			if y[a[i]] == k:
				return a[i]
		return -1


class Solution:

	def firstElementKTime(self, A, N, K):
		freq = {}
		for i in range(N):
			if A[i] in freq:
				freq[A[i]] += 1
			else:
				freq[A[i]] = 1
			if freq[A[i]] == K:
				return A[i]
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		if k == 1:
			return a[0]
		mp = {}
		for i in a:
			if i not in mp:
				mp[i] = 1
			else:
				mp[i] += 1
				if mp[i] == k:
					return i
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		freq = {}
		for i in a:
			if i in freq:
				freq[i] += 1
				if freq[i] == k:
					return i
					break
			else:
				freq[i] = 1
				if freq[i] == k:
					return i
					break
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		count = dict()
		for ele in a:
			count[ele] = count.get(ele, 0) + 1
			if count[ele] == k:
				return ele
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		mdict = {}
		for x in a:
			if x in mdict:
				mdict[x] += 1
			else:
				mdict[x] = 1
			if mdict[x] == k:
				return x
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		map1 = {}
		for i in range(n):
			if a[i] not in map1:
				map1[a[i]] = 0
			map1[a[i]] += 1
			if map1[a[i]] == k:
				return a[i]
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		c = {}
		s = 0
		for i in a:
			if i not in c:
				c[i] = s + 1
			else:
				c[i] = c[i] + 1
			if c[i] == k:
				return i
				break
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		freq = {}
		for num in a:
			freq[num] = freq.get(num, 0) + 1
			if freq[num] == k:
				return num
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		from collections import defaultdict
		d = defaultdict(int)
		for char in a:
			d[char] += 1
			if d[char] == k:
				return char
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		di = dict()
		for number in a:
			if number not in di:
				di[number] = di.get(number, 0) + 1
			elif number in di:
				di[number] += 1
			if di[number] == k:
				return number
		return -1


from collections import defaultdict

class Solution:

	def firstElementKTime(self, a, n, k):
		tmp = defaultdict(lambda : 0)
		for i in a:
			tmp[i] += 1
			if tmp[i] == k:
				return i
		return -1


class Solution:

	def firstElementKTime(self, arr, n, k):
		freq_count = {}
		for (i, num) in enumerate(arr):
			if num not in freq_count:
				freq_count[num] = 1
			else:
				freq_count[num] += 1
			if freq_count[num] == k:
				return num
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		numOccurrences = {}
		for num in a:
			if numOccurrences.get(num) is not None:
				numOccurrences[num] += 1
			else:
				numOccurrences[num] = 1
			if numOccurrences[num] == k:
				return num
		return -1


class Solution:

	def firstElementKTime(self, a, n, k):
		dict = {}
		for number in a:
			if number not in dict:
				dict[number] = 1
			else:
				dict[number] += 1
			if dict[number] >= k:
				return number
		return -1