Given an array Arr of size N, the array contains numbers in range from 0 to K-1 where K is a positive integer and K <= N. Find the maximum repeating number in this array. If there are two or more maximum repeating numbers return the element having least value.
Example 1:
Input:
N = 4, K = 3
Arr[] = {2, 2, 1, 2}
Output: 2
Explanation: 2 is the most frequent
element.
Example 2:
Input:
N = 6, K = 3
Arr[] = {2, 2, 1, 0, 0, 1}
Output: 0
Explanation: 0, 1 and 2 all have same
frequency of 2.But since 0 is smallest,
you need to return 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxRepeating() which takes the array of integers arr, n and k as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(K)
Constraints:
1 <= N <= 10^{7}
1 <= K <= N
0 <= Arr_{i} <= K - 1

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3
class Solution:

	def maxRepeating(self,arr, n, k):
		# code here

Solutions:
class Solution:

	def maxRepeating(self, arr, n, k):
		freq = [0] * k
		max_count = 0
		res = -1
		for i in range(n):
			freq[arr[i]] += 1
			if freq[arr[i]] > max_count:
				max_count = freq[arr[i]]
				res = arr[i]
			elif freq[arr[i]] == max_count and arr[i] < res:
				res = arr[i]
		return res


from collections import Counter

class Solution:

	def maxRepeating(self, arr, n, k):
		c = Counter(arr)
		sorted_c = sorted(c.items(), key=lambda x: x[1], reverse=True)
		return sorted_c[0][0]


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		c = max(d.values())
		b = []
		for (k, v) in d.items():
			if v == c:
				b.append(k)
		b.sort()
		return b[0]


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		maxx = 0
		x = 0
		for i in d:
			if d[i] > maxx:
				maxx = d[i]
				x = i
		return x


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		m = max(d.values())
		l = []
		for (x, y) in d.items():
			if y == m:
				l.append(x)
		return min(l)


class Solution:

	def maxRepeating(self, arr, n, k):
		arr.sort()
		b = {}
		for i in arr:
			if i in b:
				b[i] += 1
			else:
				b[i] = 1
		c = max(b.values())
		for i in b:
			if b[i] == c:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		h = [-1] * k
		for i in arr:
			h[i] = h[i] + 1
		max_arr = []
		max = -999999
		max_e = 0
		for i in range(len(h)):
			if h[i] > max:
				max_e = max
				max = h[i]
		return h.index(max)


class Solution:

	def maxRepeating(self, Arr, N, K):
		freq = [0] * K
		for i in range(N):
			freq[Arr[i]] += 1
		max_freq = 0
		max_num = -1
		for i in range(K):
			if freq[i] > max_freq:
				max_freq = freq[i]
				max_num = i
		return max_num


from collections import Counter

class Solution:

	def maxRepeating(self, arr, n, k):
		c = Counter(arr)
		M = max(c.values())
		for i in c.keys():
			if c[i] == M:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		maps = dict()
		for i in range(len(arr)):
			if arr[i] in maps:
				maps[arr[i]] += 1
			else:
				maps[arr[i]] = 1
		maxi = 0
		for i in maps:
			maxi = max(maxi, maps[i])
		lst = []
		for i in maps:
			if maxi == maps[i]:
				lst.append(i)
			else:
				pass
		if lst:
			return min(lst)
		else:
			return list(maps.keys())[list(maps.values()).index(maxi)]


class Solution:

	def maxRepeating(self, arr, n, k):
		for i in range(n):
			arr[arr[i] % n] += n
		ans1 = -1
		for i in range(k):
			if arr[i] // n > ans1:
				ans1 = arr[i] // n
				ans = i
		return ans


class Solution:

	def maxRepeating(self, arr, n, k):
		dic = {}
		for i in arr:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		co = 0
		min = 10000000000000
		for i in dic:
			if co < dic[i]:
				co = dic[i]
				min = i
			if co == dic[i]:
				if min > i:
					min = i
		return min


import collections

class Solution:

	def maxRepeating(self, arr, n, k):
		d1 = collections.Counter(arr)
		m = max(d1.values())
		for i in d1:
			if d1[i] == m:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		map = {}
		for i in arr:
			map[i] = map.get(i, 0) + 1
		maxind = 0
		maxval = []
		for (val, ind) in map.items():
			if ind > maxind:
				maxind = ind
				maxval = val
		return maxval


class Solution:

	def maxRepeating(self, arr, n, k):
		(res, d) = (0, {})
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		count = -1
		mini = 100000000
		for (key, value) in d.items():
			if value > count:
				count = value
				mini = key
			elif value == count:
				count = value
				if key < mini:
					mini = key
		return mini


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		max = 0
		res = 0
		for i in d:
			if d[i] > max:
				max = d[i]
				res = i
		return res


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for num in arr:
			d[num] = d.get(num, 0) + 1
		dl = sorted(d.items(), key=lambda x: x[1], reverse=True)
		return dl[0][0]


class Solution:

	def maxRepeating(self, arr, n, k):
		s = list(set(arr))
		d = {}
		for i in s:
			d[i] = 0
		for i in arr:
			d[i] += 1
		keymax = max(d, key=lambda x: d[x])
		return keymax


class Solution:

	def maxRepeating(self, arr, n, k):
		dict = {}
		for i in arr:
			if i in dict:
				dict[i] += 1
			else:
				dict[i] = 1
		x = 0
		max = 0
		for i in dict:
			if dict[i] > max:
				max = dict[i]
				x = i
		return x


from collections import Counter

class Solution:

	def maxRepeating(self, arr, n, k):
		d = Counter(arr)
		x = max(d.values())
		for i in d.keys():
			if d[i] == x:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		a = []
		d = dict()
		for ele in arr:
			d[ele] = 0
		for ele in arr:
			d[ele] = d[ele] + 1
		maxi = max(d.values())
		for (k, v) in d.items():
			if v == maxi:
				a.append(k)
		a.sort()
		return a[0]


class Solution:

	def maxRepeating(self, arr, n, k):
		test = {}
		for i in range(0, n):
			if arr[i] in test.keys():
				test[arr[i]] += 1
			if arr[i] not in test.keys():
				test[arr[i]] = 1
		a = []
		max1 = max(test.values())
		for i in test:
			if test[i] == max1:
				a.append(i)
		return min(a)


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		m = 0
		for j in d:
			if d[j] > m:
				m = d[j]
				x = j
		return x


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		l = []
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		mx = max(d.values())
		for key in d:
			if d[key] == mx:
				l.append(key)
		return min(l)


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		c = -1
		mini = 10000000000
		for (key, value) in d.items():
			if value > c:
				c = value
				mini = key
			if value == c:
				c = value
				if key < mini:
					mini = key
		return mini


class Solution:

	def maxRepeating(self, arr, n, k):
		dict1 = {}
		lst = []
		for i in arr:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		maxi = max(dict1.values())
		for i in dict1:
			if dict1[i] == maxi:
				lst.append(i)
		return lst[0]


class Solution:

	def maxRepeating(self, arr, n, k):
		mx = 0
		for i in arr:
			if i > mx:
				mx = i
		tab = [0 for i in range(0, mx + 1)]
		for i in arr:
			tab[i] += 1
		mx = max(tab)
		return tab.index(mx)


class Solution:

	def maxRepeating(self, arr, n, k):
		b = [0] * k
		for i in arr:
			b[i] += 1
		return b.index(max(b))


class Solution:

	def maxRepeating(self, arr, n, k):
		freq = [0] * k
		for i in arr:
			freq[i] += 1
		ma = freq[0]
		mi = 0
		for i in range(1, k):
			if freq[i] > ma:
				ma = freq[i]
				mi = i
		return mi


class Solution:

	def maxRepeating(self, arr, n, k):
		dict = {}
		for i in arr:
			if i not in dict:
				dict[i] = 1
			else:
				dict[i] += 1
		myvalues = list(dict.values())
		myvalues.sort(reverse=True)
		max = myvalues[0]
		mykeys = list(dict.keys())
		mykeys.sort()
		dict_sort = {i: dict[i] for i in mykeys}
		for i in dict_sort:
			if dict_sort[i] == max:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		max_repeat = 0
		max_repeat_num = 0
		dict_ = {}
		for elem in arr:
			dict_.update({elem: dict_.get(elem, 0) + 1})
			if dict_[elem] > max_repeat:
				max_repeat = dict_[elem]
				max_repeat_num = elem
			elif dict_[elem] == max_repeat and elem < max_repeat_num:
				max_repeat_num = elem
		return max_repeat_num


class Solution:

	def maxRepeating(self, arr, n, k):
		arr.sort()
		count = {x: 0 for x in arr}
		max = 0
		for x in arr:
			count[x] += 1
			if max <= count[x]:
				max = count[x]
		for (key, values) in count.items():
			if values == max:
				return key


class Solution:

	def maxRepeating(self, arr, n, k):
		dict = {}
		for i in arr:
			if i not in dict:
				dict[i] = 1
			else:
				dict[i] += 1
		m = max(dict.values())
		l = [i for i in dict if dict[i] == m]
		l.sort()
		return l[0]


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		c = max(d.values())
		min = k + 1
		for i in d:
			if d[i] == c and i < min:
				min = i
		return min


class Solution:

	def maxRepeating(self, arr, n, k):
		freq = {}
		for i in arr:
			try:
				freq[i] = freq.get(i, 0) + 1
			except:
				freq[i] = 1
		max_freq = max(freq.values())
		max_numbers = []
		for (k, v) in freq.items():
			if v == max_freq:
				max_numbers.append(k)
		return min(max_numbers)


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		v = []
		g = []
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in d.values():
			v.append(i)
		arr.sort()
		arr = set(arr)
		max_f = max(v)
		for i in arr:
			if d[i] == max_f:
				g.append(i)
		return g[0]


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		g = max(d.values())
		for (i, j) in d.items():
			if j == g:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		output = [0] * k
		highest = 0
		for num in arr:
			output[num] += 1
			highest = max(highest, output[num])
		for (i, item) in enumerate(output):
			if item == highest:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		dp = {}
		for i in arr:
			if i in dp:
				dp[i] += 1
			else:
				dp[i] = 1
		count = -1
		mini = 100000000
		for (key, value) in dp.items():
			if value > count:
				count = value
				mini = key
			elif value == count:
				count = value
				if key < mini:
					mini = key
		return mini


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		mx = 0
		mx_ele = arr[0]
		for i in range(len(arr)):
			if arr[i] in d:
				d[arr[i]] += 1
			else:
				d[arr[i]] = 1
			if mx < d[arr[i]]:
				mx = d[arr[i]]
				mx_ele = arr[i]
			elif mx == d[arr[i]]:
				mx_ele = min(mx_ele, arr[i])
		return mx_ele


class Solution:

	def maxRepeating(self, arr, n, k):
		m = {}
		for i in arr:
			if i not in m:
				m[i] = 1
			else:
				m[i] += 1
		x = max(m.values())
		for i in m.keys():
			if m[i] == x:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		count = [0] * k
		for item in arr:
			count[item] += 1
		maximum = -1
		minimum_element = k
		for i in range(k):
			if count[i] > maximum:
				maximum = count[i]
				minimum_element = i
			elif count[i] == maximum:
				if minimum_element > i:
					minimum_element = i
		return minimum_element


class Solution:

	def maxRepeating(self, arr, n, k):
		from collections import Counter
		d = Counter(arr)
		m = max(list(d.values()))
		a = []
		for (i, j) in d.items():
			if j == m:
				a.append(i)
		return min(a)


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		mx = 0
		for (a, b) in d.items():
			if b > mx:
				mx = b
				out = a
		return out


class Solution:

	def maxRepeating(self, arr, n, k):
		for i in range(n):
			original_index = arr[i] % (n + 1)
			arr[original_index] = arr[original_index] + (n + 1)
		ans = 0
		freq = arr[0] // (n + 1)
		for i in range(1, n):
			actual_freq = arr[i] // (n + 1)
			if freq < actual_freq and actual_freq != 0:
				freq = actual_freq
				ans = i
		return ans


class Solution:

	def maxRepeating(self, arr, n, k):
		sc = [0 for i in range(k + 1)]
		for i in arr:
			sc[i] += 1
		maxi = 0
		ele = 0
		for i in range(k + 1):
			if sc[i] > maxi:
				maxi = sc[i]
				ele = i
		return ele


class Solution:

	def maxRepeating(self, arr, n, k):
		maxcount = 0
		maxele = arr[0]
		count = [0] * k
		for i in range(n):
			count[arr[i]] += 1
			if count[arr[i]] > maxcount:
				maxcount = count[arr[i]]
				maxele = arr[i]
			elif count[arr[i]] == maxcount:
				maxele = min(arr[i], maxele)
		return maxele


class Solution:

	def maxRepeating(self, arr, n, k):
		dic = {k: 0 for k in range(0, k)}
		for i in arr:
			dic[i] += 1
		a = min(dic.items(), key=lambda x: (-x[1], x[0]))
		return a[0]


class Solution:

	def maxRepeating(self, arr, n, k):
		d = dict()
		for x in arr:
			d[x] = d.get(x, 0) + 1
		arr.sort(key=lambda x: [d[x], -x])
		return arr[-1]


from collections import Counter

class Solution:

	def maxRepeating(self, arr, n, k):
		d = Counter(arr)
		x = 9999999999999
		y = 0
		for (i, j) in d.items():
			if j > y:
				y = j
				x = i
			elif j == y:
				if i < x:
					x = i
				else:
					pass
			else:
				pass
		return x


class Solution:

	def maxRepeating(self, arr, n, k):
		hashmap = {}
		m = 0
		for i in arr:
			hashmap[i] = hashmap.get(i, 0) + 1
			m = max(hashmap[i], m)
		l = []
		for (i, j) in hashmap.items():
			if j == m:
				l.append(i)
		return min(l)


class Solution:

	def maxRepeating(self, arr, n, k):
		a = {}
		repeat = []
		for i in arr:
			if i not in a:
				a.update({i: 1})
			else:
				a[i] += 1
		lst = a.values()
		lst = max(lst)
		for i in a:
			if a[i] == lst:
				repeat.append(i)
		return min(repeat)


class Solution:

	def maxRepeating(self, arr, n, k):
		s = {}
		for i in arr:
			if i in s:
				s[i] += 1
			else:
				s[i] = 1
		max_ = 1
		for i in s:
			if max_ < s[i]:
				max_ = s[i]
				ele = i
		return ele


class Solution:

	def maxRepeating(self, arr, n, k):
		h = [0] * k
		for i in arr:
			h[i] = h[i] + 1
		max_freq = 0
		idx = 9999999
		for i in range(len(h)):
			if h[i] > max_freq:
				max_freq = h[i]
				idx = i
		return idx


class Solution:

	def maxRepeating(self, arr, n, k):
		mp = dict()
		mx = 0
		mx_ele = arr[0]
		for i in range(n):
			if arr[i] in mp:
				mp[arr[i]] += 1
			else:
				mp[arr[i]] = 1
			if mx < mp[arr[i]]:
				mx = mp[arr[i]]
				mx_ele = arr[i]
			elif mx == mp[arr[i]]:
				mx_ele = min(mx_ele, arr[i])
		return mx_ele


from collections import defaultdict, OrderedDict

class Solution:

	def maxRepeating(self, arr, n, k):
		def_dict = defaultdict()
		res = []
		for i in arr:
			if i in def_dict:
				def_dict[i] += 1
			else:
				def_dict[i] = 1
		max_val = max(def_dict.values())
		ord_dict = OrderedDict.fromkeys(arr)
		for j in ord_dict:
			if def_dict[j] == max_val:
				res.append(j)
		return min(res)


class Solution:

	def maxRepeating(self, arr, n, k):
		m = 0
		for i in arr:
			if i > m:
				m = i
		t = [0 for i in range(0, m + 1)]
		for i in arr:
			t[i] += 1
		m = max(t)
		return t.index(m)


class Solution:

	def maxRepeating(self, arr, n, k):
		max1 = 0
		max_el = arr[0]
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			if max1 < d[i]:
				max1 = d[i]
				max_el = i
			elif max1 == d[i]:
				max_el = min(max_el, i)
		return max_el


class Solution:

	def maxRepeating(self, arr, n, k):
		frequency_map = {}
		for num in arr:
			if num in frequency_map.keys():
				frequency_map[num] += 1
			else:
				frequency_map[num] = 1
		max_freq = 0
		max_freq_elem = arr[0]
		for key in frequency_map:
			if frequency_map[key] > max_freq:
				max_freq = frequency_map[key]
				max_freq_elem = key
		return max_freq_elem


class Solution:

	def maxRepeating(self, arr, n, k):
		extra = [0] * k
		for i in range(n):
			extra[arr[i]] += 1
		maxi_index = 0
		for j in range(k):
			if extra[j] > extra[maxi_index]:
				maxi_index = j
		return maxi_index


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		e = dict(sorted(d.items()))
		l1 = list(e.keys())
		l2 = list(e.values())
		return l1[l2.index(max(l2))]


class Solution:

	def maxRepeating(self, arr, n, k):
		dic = {}
		for i in arr:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		d = max(dic.values())
		for i in dic:
			if dic[i] == d:
				return i


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in arr:
			d[i] = 1 + d.get(i, 0)
		f = [[] for i in range(n + 1)]
		for (i, j) in d.items():
			f[j].append(i)
		for i in range(len(f) - 1, -1, -1):
			if f[i]:
				return min(f[i])


class Solution:

	def maxRepeating(self, arr, n, k):
		times = {}
		maxi = 1
		for num in arr:
			if num not in times:
				times[num] = 1
			else:
				times[num] += 1
				maxi = max(maxi, times[num])
		res = 10 ** 10
		for num in times:
			if times[num] == maxi and res > num:
				res = num
		return res


class Solution:

	def maxRepeating(self, arr, n, k):
		dict = {}
		for i in range(0, n):
			val = arr[i]
			try:
				dict[val] = dict[val] + 1
			except:
				dict[val] = 1
		maxx = 0
		val = dict.get(0)
		for (i, j) in dict.items():
			if j > maxx:
				maxx = j
				val = i
		return val


class Solution:

	def maxRepeating(self, arr, n, k):
		d = {}
		for i in range(len(arr)):
			if d.get(arr[i], -1) == -1:
				d[arr[i]] = 1
			else:
				d[arr[i]] += 1
		maxfreq = 0
		for key in d:
			if d[key] > maxfreq:
				maxfreq = d[key]
				ans = key
			if d[key] == maxfreq:
				ans = min(ans, key)
		return ans


class Solution:

	def maxRepeating(self, arr, n, k):
		hashmap = {}
		for i in arr:
			if i in hashmap:
				hashmap[i] += 1
			else:
				hashmap[i] = 1
		maxcount = 0
		digit = -1
		for i in hashmap:
			if hashmap[i] > maxcount:
				maxcount = hashmap[i]
				digit = i
			elif hashmap[i] == maxcount and i < digit:
				digit = i
		return digit