Given a number N, find the most occurring digit in it. If two or more digits occur same number of times, then return the highest of them. Input number is given as a string.
Example 1:
Input:
N = "12234"
Output:
2
Explanation :
2 occurs most times
Example 2:
Input:
N = "1122"
Output:
2
Explanation :
1 and 2 occurs most times but 2 > 1.
 
Your Task: 
You don't need to read input or print anything. Your task is to complete the function solve() that receives a string N as input parameter and returns the most frequent digit in it as a string.
Expected Time Complexity : O(len(N))
Expected Space Complexity : O(1)
Constraints:
1<= len(N) <=101000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:

    def solve(self, N):
        # code here

Solutions:
class Solution:

	def solve(self, N):
		d = {}
		m = 1
		for i in N:
			if i in d:
				d[i] += 1
				if d[i] > m:
					m = d[i]
			else:
				d[i] = 1
		b = []
		for i in N:
			if d[i] == m:
				b.append(int(i))
		return max(b)


class Solution:

	def solve(self, n):
		d = {}
		for t in n:
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
		mx = 0
		for i in d:
			if d[i] > mx:
				c = int(i)
				mx = d[i]
		for i in d:
			if d[i] == mx:
				if int(i) > c:
					c = int(i)
		return c


class Solution:

	def solve(self, N):
		d = {}
		for ele in N:
			d[ele] = 0
		for ele in N:
			d[ele] += 1
		maxi = 0
		for ele in d.keys():
			if d[ele] > maxi:
				maxi = d[ele]
		z = []
		for ele in d.keys():
			if d[ele] == maxi:
				z.append(ele)
		return max(z)


class Solution:

	def solve(self, N):
		freq = {}
		for i in N:
			if i not in freq:
				freq[i] = 0
			freq[i] += 1
		m = mi = 0
		for i in freq:
			if m < freq[i]:
				m = freq[i]
				mi = i
			elif m == freq[i]:
				mi = max(mi, i)
		return mi


class Solution:

	def solve(self, N):
		f = -1
		m = -1
		for i in N:
			if N.count(i) > f:
				m = i
				f = N.count(i)
			elif N.count(i) == f:
				if int(i) > int(m):
					m = i
			else:
				pass
		return m


class Solution:

	def solve(self, N):
		d = {}
		for i in N:
			if i not in d:
				d[i] = 0
			d[i] += 1
		m = max(list(d.values()))
		l = []
		for i in d.keys():
			if d[i] == m:
				l.append(int(i))
		return max(l)


import collections

class Solution:

	def solve(self, N):
		dict1 = collections.Counter(N)
		m = []
		for i in dict1:
			if dict1[i] == max(dict1.values()):
				m.append(i)
		return max(m)


class Solution:

	def solve(self, N):
		l = [0] * 10
		for i in N:
			l[int(i)] += 1
		max1 = 0
		res = 0
		for i in range(10):
			if l[i] >= max1:
				res = i
				max1 = l[i]
		return res


class Solution:

	def solve(self, N):
		d = {}
		for i in N:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		s = max(d.values())
		res = 0
		for (i, j) in d.items():
			if j == s:
				res = max(res, int(i))
		return res


class Solution:

	def solve(self, n):
		d = {}
		for i in n:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		a = {}
		dv = max(d.values())
		for (k, v) in d.items():
			if v == dv:
				a[k] = v
		ak = list(a.keys())
		return max(ak)


class Solution:

	def solve(self, N):
		x = int(max(N))
		lis = [0] * (x + 1)
		for i in range(len(N)):
			lis[int(N[i])] += 1
		y = max(lis)
		for j in range(len(lis)):
			if lis[j] == y:
				maxm = j
		return maxm


class Solution:

	def solve(self, N):
		lst = []
		d = {}
		for i in N:
			lst.append(i)
		for i in lst:
			if i not in d:
				d[i] = 0
			d[i] += 1
		x = max(d.values())
		ans = []
		for i in d:
			if d[i] == x:
				ans.append(int(i))
		return max(ans)


class Solution:

	def solve(self, n):
		n = list(n)
		d = dict()
		for i in n:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		n.sort(key=lambda x: (d[x], x))
		return n[-1]


class Solution:

	def solve(self, N):
		m = 0
		for i in N:
			m = max(m, N.count(i))
		k = []
		for i in N:
			if m == N.count(i):
				k.append(int(i))
		return max(k)


class Solution:

	def solve(self, N):
		if len(N) == 1:
			return N
		(m, d, fd) = (0, {}, '')
		for i in N:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			if d[i] > m:
				m = d[i]
				fd = i
			elif d[i] == m:
				if int(i) > int(fd):
					fd = i
		return fd


from collections import Counter

class Solution:

	def solve(self, N):
		d = Counter(N)
		m = max(d.values())
		maxi = 0
		q = 0
		for i in d:
			if d[i] > m:
				maxi = m
				q = int(i)
			elif d[i] == m:
				if int(i) > q:
					q = int(i)
		return q


from collections import Counter

class Solution:

	def solve(self, N):
		out = -1
		freq = -1
		N = str(N)
		for i in N:
			if N.count(i) > freq:
				out = i
				freq = N.count(i)
			elif N.count(i) == freq:
				if int(i) > int(out):
					out = i
			else:
				pass
		return out


class Solution:

	def solve(self, N):
		from collections import Counter
		di = Counter(N)
		maxi = num = -1
		for i in di:
			temp = di[i]
			if temp > maxi:
				maxi = temp
				num = i
			if temp == maxi:
				num = max(i, num)
		return num


class Solution:

	def solve(self, N):
		d = {}
		for i in N:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		d1 = sorted(d.values())
		a = d1[-1]
		l = []
		for i in d:
			if d[i] == a:
				l.append(int(i))
		l.sort()
		return l[-1]


class Solution:

	def solve(self, N):
		Map = {}
		for i in N:
			k = int(i)
			if k in Map:
				Map[k] += 1
			else:
				Map[k] = 1
		MaxFreq = 0
		MaxVal = 0
		for i in Map:
			if Map[i] > MaxFreq:
				MaxVal = i
				MaxFreq = Map[i]
			elif Map[i] == MaxFreq:
				MaxVal = max(MaxVal, int(i))
		return MaxVal


class Solution:

	def solve(self, N):
		d = {}
		for ele in N:
			d[ele] = 0
		for ele in N:
			d[ele] = d[ele] + 1
		maxx = 0
		for (k, v) in d.items():
			if v >= maxx:
				maxx = v
		l = []
		for (k, v) in d.items():
			if v == maxx:
				l.append(k)
		return max(l)


from collections import Counter

class Solution:

	def solve(self, N):
		c = Counter(N)
		maxi = max(c.values())
		return sorted((item for item in c if c[item] == maxi))[-1]


class Solution:

	def solve(self, N):
		from collections import Counter
		f = Counter(N)
		count = max(f.values())
		ans = max(N)
		if count > 1:
			a = []
			for v in f:
				if f[v] == count:
					a.append(v)
			ans = max(a)
		return ans


class Solution:

	def solve(self, N):
		max = 0
		no = 0
		for i in N:
			count = N.count(i)
			if max <= count:
				if max == count:
					if no <= i:
						no = i
				else:
					no = i
					max = count
		return no


class Solution:

	def solve(self, N):
		dic = {}
		for i in N:
			i = int(i)
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		a = sorted(dic.items(), key=lambda x: (x[1], x[0]))
		return a[-1][0]


class Solution:

	def solve(self, N):
		mapp = {0: 0}
		for i in range(10):
			mapp[i] = 0
		for ch in N:
			mapp[int(ch)] = mapp.get(int(ch), 0) + 1
		maxi = 0
		ans = 0
		for i in range(10):
			ch = int(i)
			if mapp[ch] > maxi:
				maxi = mapp[ch]
				ans = ch
			elif maxi == mapp[ch]:
				ans = ch
		return ans


class Solution:

	def solve(self, N: str) -> str:
		counter = {}
		for digit in N:
			if digit in counter:
				counter[digit] += 1
			else:
				counter[digit] = 1
		max_count = 0
		for value in counter.values():
			if value > max_count:
				max_count = value
		return max((key for (key, value) in counter.items() if value == max_count))


class Solution:

	def solve(self, N):
		res = []
		d = {}
		for i in N:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		ma = max(d.values())
		for i in d:
			if d[i] == ma:
				res.append(i)
		return max(res)


class Solution:

	def solve(self, N):
		d = {}
		for i in str(N):
			try:
				d[i] += 1
			except:
				d[i] = 1
		l = []
		mx = max(d.values())
		for i in d:
			if d[i] == mx:
				l.append(int(i))
		return max(l)


class Solution:

	def solve(self, N):
		d = {}
		l = []
		l1 = []
		l2 = []
		for i in N:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in d:
			l.append(d[i])
		m = max(l)
		for i in d:
			if d[i] == m:
				l1.append(i)
		return max(l1)


class Solution:

	def solve(self, N):
		a = -1
		b = -1
		for i in N:
			if N.count(i) > a:
				b = i
				a = N.count(i)
			elif N.count(i) == a:
				if int(i) > int(b):
					b = i
			else:
				pass
		return b


class Solution:

	def solve(self, N):
		k = -1
		z = -1
		for i in N:
			if N.count(i) > k:
				z = i
				k = N.count(i)
			elif N.count(i) == k:
				if int(i) > int(z):
					z = i
			else:
				pass
		return z


class Solution:

	def solve(self, N):
		c = [0] * 10
		max_num = 0
		res = -1
		for i in N:
			pos = ord(i) - ord('0')
			c[pos] += 1
		for i in range(10):
			if c[i] > 0:
				if c[i] >= max_num:
					max_num = c[i]
					res = i
		return res


class Solution:

	def solve(self, N):
		c = 0
		s = {}
		for i in N:
			if i in s:
				s[i] += 1
			else:
				s[i] = 1
		for i in s:
			if s[i] == c:
				if i > g:
					g = i
			if s[i] > c:
				c = s[i]
				g = i
		return g


class Solution:

	def solve(self, N):
		s = {}
		for i in N:
			if i in s:
				s[i] += 1
			else:
				s[i] = 1
		mx = -1
		l = []
		for i in s:
			if s[i] == mx:
				l.append(i)
			elif s[i] > mx:
				mx = s[i]
				l = [i]
		return max(l)


class Solution:

	def solve(self, N):
		ls = [0] * 10
		for i in N:
			ls[int(i)] += 1
		m = max(ls)
		i = ls.index(m)
		for k in range(i + 1, 10):
			if ls[k] == m:
				i = k
		return i


class Solution:

	def solve(self, N):
		f = -1
		m = -1
		for i in N:
			if N.count(i) > f:
				m = i
				f = N.count(i)
			elif N.count(i) == f:
				if int(i) > int(m):
					m = i
			else:
				pass
		return m
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = input()
		solObj = Solution()
		print(solObj.solve(N))


class Solution:

	def solve(self, N):
		list = []
		for item in N:
			if item not in list:
				list.append(item)
		count_max = list[0]
		for i in range(0, len(list)):
			if N.count(list[i]) > N.count(count_max):
				count_max = list[i]
			elif N.count(list[i]) == N.count(count_max):
				count_max = max(list[i], count_max)
		return count_max


class Solution:

	def solve(self, input):
		m = {}
		k_max = ''
		v_max = 0
		for ch in input:
			m[ch] = m.get(ch, 0) + 1
		for (k, v) in m.items():
			if v > v_max:
				v_max = v
				k_max = k
			if v == v_max:
				k_max = str(max(int(k), int(k_max)))
		return k_max


class Solution:

	def solve(self, N):
		list1 = list(N)
		list2 = []
		for items in list1:
			k = list1.count(items)
			list2.append(k)
		list2.sort()
		max_value = list2[-1]
		answer = []
		for items in list1:
			if list1.count(items) == max_value:
				answer.append(items)
		answer.sort()
		return answer[-1]


class Solution:

	def solve(self, N):
		hmap = {}
		for i in N:
			if i in hmap:
				hmap[i] += 1
			else:
				hmap[i] = 1
		arr = sorted(hmap, reverse=True, key=lambda x: hmap[x])
		max = hmap[arr[0]]
		max_num = arr[0]
		for a in arr:
			if max == hmap[a]:
				if max_num <= a:
					max_num = a
			else:
				break
		return max_num


from collections import Counter

class Solution:

	def solve(self, N):
		z = Counter(N)
		x = []
		for (i, j) in z.items():
			x.append(int(j))
		l = max(x)
		m = []
		for (i, j) in z.items():
			if z[i] == l:
				m.append(int(i))
		return max(m)


class Solution:

	def solve(self, N):
		from collections import Counter
		a = Counter(N)
		b = list(a.keys())
		c = list(a.values())
		d = max(c)
		e = c.count(d)
		if e == 1:
			f = c.index(d)
			g = b[f]
		if e > 1:
			h = []
			for (i, j) in a.items():
				if j == d:
					h.append(i)
			g = max(h)
		return g


class Solution:

	def solve(self, N):
		zero = 0
		one = 0
		two = 0
		three = 0
		four = 0
		five = 0
		six = 0
		seven = 0
		eight = 0
		nine = 0
		for i in N:
			if i == '0':
				zero += 1
			elif i == '1':
				one += 1
			elif i == '2':
				two += 1
			elif i == '3':
				three += 1
			elif i == '4':
				four += 1
			elif i == '5':
				five += 1
			elif i == '6':
				six += 1
			elif i == '7':
				seven += 1
			elif i == '8':
				eight += 1
			elif i == '9':
				nine += 1
		x = max(zero, one, two, three, four, five, six, seven, eight, nine)
		if nine == x:
			return '9'
		elif eight == x:
			return '8'
		elif seven == x:
			return '7'
		elif six == x:
			return '6'
		elif five == x:
			return '5'
		elif four == x:
			return '4'
		elif three == x:
			return '3'
		elif two == x:
			return '2'
		elif one == x:
			return '1'
		elif zero == x:
			return '0'


class Solution:

	def solve(self, N):
		d = {}
		u = []
		v = []
		for k in N:
			d[k] = d.get(k, 0) + 1
		for (x, y) in d.items():
			u.append(y)
		j = max(u)
		for (x, y) in d.items():
			if y == j:
				v.append(x)
		v = sorted(v)
		return v[-1]


class Solution:

	def solve(self, N):
		fre = co = -1
		c = {}
		for j in N:
			c[j] = N.count(j)
			if fre < c[j]:
				co = j
				fre = c[j]
			elif fre == c[j]:
				if co < j:
					co = j
		return co


class Solution:

	def solve(self, N):
		l = list(N)
		d = {}
		l1 = []
		l2 = []
		count = 0
		for x in range(len(N)):
			if l[x] not in d:
				d[l[x]] = 1
			else:
				d[l[x]] += 1
		for x in d.values():
			l1.append(x)
		maxx = max(l1)
		for (k, v) in d.items():
			if v == maxx:
				l2.append(k)
		maxx2 = max(l2)
		return maxx2


class Solution:

	def solve(self, N):
		k = sorted(list(set(list(N))), reverse=True)
		d = []
		for i in k:
			d.append([N.count(i), i])
		return sorted(d, reverse=True)[0][1]


class Solution:

	def solve(self, N):
		d = {}
		for i in N:
			d.setdefault(i, 0)
			d[i] += 1
		l = list(d.values())
		maxi = max(l)
		ans = []
		for (i, j) in d.items():
			if j == maxi:
				ans.append(i)
		return max(ans)


class Solution:

	def solve(self, N):
		dic = {}
		for i in N:
			try:
				dic[i] += 1
			except:
				dic[i] = 1
		mx = -1
		l = []
		for i in dic:
			if dic[i] == mx:
				l.append(i)
			if dic[i] > mx:
				mx = dic[i]
				l = [i]
		return max(l)


class Solution:

	def solve(self, n):
		lst = []
		d = {}
		for x in n:
			if x in d:
				d[x] += 1
			else:
				d[x] = 1
		k = max(d.values())
		for key in list(d.keys()):
			if d[key] == k:
				lst.append(key)
		return int(max(lst))


from collections import Counter

class Solution:

	def solve(self, N):
		gg = Counter(N)
		ans = ['-1']
		m = max(gg.values())
		for i in gg:
			if gg[i] == m:
				ans.append(i)
		return max(ans)


class Solution:

	def solve(self, N):
		N = N[::-1]
		a = [0] * 10
		for i in N:
			a[int(i)] += 1
		c = 0
		max1 = a[0]
		for i in range(len(a)):
			if a[i] >= max1:
				max1 = a[i]
				c = i
		return c


class Solution:

	def solve(self, N):
		arr = [0] * 10
		for i in range(len(N)):
			arr[int(N[i])] += 1
		m = 0
		for i in range(10):
			if m <= arr[i]:
				m = arr[i]
				ans = i
		return ans


class Solution:

	def solve(self, N):
		d = dict()
		for i in range(len(N)):
			if int(N[i]) in d:
				d[int(N[i])] += 1
			else:
				d[int(N[i])] = 1
		ele = 0
		m = 0
		for (k, v) in d.items():
			if v > m:
				m = v
				ele = k
			elif v == m and k >= ele:
				m = v
				ele = k
		return ele


class Solution:

	def solve(self, N):
		h_map = [0 for i in range(10)]
		ct = 0
		c = 0
		for i in N:
			n = int(i)
			h_map[n] += 1
		for i in range(10):
			if h_map[i] >= c:
				c = h_map[i]
				ct = i
		return ct


class Solution:

	def solve(self, N):
		dict1 = {}
		for i in N:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		z = sorted(dict1.items(), key=lambda x: x[1], reverse=True)
		max1 = z[0][1]
		m = 0
		for i in z:
			if i[1] == max1:
				if m < int(i[0]):
					m = int(i[0])
		return m


class Solution:

	def solve(self, number):
		from collections import Counter
		number_dict = Counter(number)
		max_occ = max(number_dict.values())
		num = max([item[0] for item in number_dict.items() if item[1] == max_occ])
		return num


class Solution:

	def solve(self, N):
		d = {}
		for i in N:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		d = sorted(d.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
		return d[0][0]


class Solution:

	def solve(self, N):
		N = list(N)
		from collections import Counter
		d = Counter(N)
		m = 0
		ans = 0
		for i in d:
			if d[i] > m:
				m = d[i]
				ans = int(i)
			elif d[i] == m:
				ans = max(ans, int(i))
		return ans


class Solution:

	def solve(self, N):
		d = dict()
		for i in N:
			d[i] = d.get(i, 0) + 1
		maxi = 0
		for (k, v) in d.items():
			if v > maxi:
				maxi = v
		res = []
		for (k, v) in d.items():
			if v == maxi:
				res.append(int(k))
		return max(res)


class Solution:

	def solve(self, N):
		a = list(N)
		counter = {}
		for i in a:
			counter.setdefault(i, 0)
			counter[i] += 1
		m = -1
		prevocc = 0
		for key in counter:
			if counter[key] >= prevocc:
				prevocc = counter[key]
		for key in counter:
			if counter[key] == prevocc and int(key) > m:
				m = int(key)
		return m


class Solution:

	def solve(self, N):
		a = []
		b = []
		c = []
		for i in N:
			a.append(int(i))
		for i in a:
			b.append(a.count(i))
		m = b[0]
		for i in b:
			if i > m:
				m = i
		for i in range(0, len(N)):
			if b[i] == m:
				c.append(i)
		m = a[c[0]]
		for i in c:
			if a[i] > m:
				m = a[i]
		return m


class Solution:

	def solve(self, n):
		temp = {}
		for i in n:
			temp[i] = temp.get(i, 0) + 1
		res = None
		for i in temp:
			if res == None or temp[i] > temp[res]:
				res = i
			elif temp[i] == temp[res]:
				if i > res:
					res = i
		return res


class Solution:

	def solve(self, N):
		(n, Max) = ([0] * 10, 0)
		for c in N:
			n[ord(c) - ord('0')] += 1
		for i in n:
			if i > Max:
				Max = i
		for i in range(9, -1, -1):
			if n[i] == Max:
				return i
		return None


class Solution:

	def solve(self, N):
		e = sorted(list(set(N)))
		t = []
		for i in e:
			t.append((N.count(i), int(i)))
		u = sorted(t)
		return u[-1][-1]


class Solution:

	def solve(self, N):
		d = {}
		for i in N:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		most_freq = 0
		res = 0
		for i in d:
			if d[i] > most_freq:
				most_freq = d[i]
				res = i
			elif d[i] == most_freq and i > res:
				res = i
		return res


class Solution:

	def solve(self, N):
		a = sorted(N)
		m = {}
		for i in N:
			if i in m:
				m[i] = m[i] + 1
			else:
				m[i] = 0
		a = m.values()
		c = []
		for i in m:
			if m[i] == max(a):
				c.append(i)
		return max(c)


class Solution:

	def solve(self, N):
		D = dict()
		for i in N:
			if i in D:
				D[i] += 1
			else:
				D[i] = 1
		Keymax = max(zip(D.values(), D.keys()))[1]
		return Keymax


class Solution:

	def solve(self, N):
		N = sorted(N)
		count = 0
		res = 0
		for i in set(N):
			if N.count(i) > count:
				count = N.count(i)
				res = i
			elif N.count(i) == count:
				res = max(res, i)
		return res


class Solution:

	def solve(self, N):
		dic = {}
		for i in N:
			if i in dic.keys():
				dic[i] += 1
			else:
				dic[i] = 1
		ans = 0
		mxOccur = max(dic.values())
		for (key, value) in dic.items():
			if value == mxOccur:
				ans = max(ans, int(key))
		return ans


class Solution:

	def solve(self, N):
		a = [0] * 10
		for i in N:
			a[9 - int(i)] += 1
		m = 0
		b = -1
		for i in range(10):
			if a[i] > m:
				b = i
				m = a[i]
		return 9 - b


class Solution:

	def solve(self, N):
		d = {}
		for i in N:
			if int(i) in d:
				d[int(i)] += 1
			else:
				d[int(i)] = 1
		m = max(d.values())
		for i in sorted(d.keys())[::-1]:
			if m == d[i]:
				return i


from collections import Counter

class Solution:

	def solve(self, N):
		count = Counter(N)
		result = 0
		max_count = 1
		for d in range(10):
			d = str(d)
			c = count[d]
			if c >= max_count:
				max_count = c
				result = d
		return result