Given a string str of lowercase alphabets. The task is to find the maximum occurring character in the string str. If more than one character occurs the maximum number of time then print the lexicographically smaller character.
Example 1:
Input:
str = testsample
Output: e
Explanation: e is the character which
is having the highest frequency.
Example 2:
Input:
str = output
Output: t
Explanation:  t and u are the characters
with the same frequency, but t is
lexicographically smaller.
Your Task:
The task is to complete the function getMaxOccuringChar() which returns the character which is most occurring.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).
Note: N = |s|
Constraints:
1 ≤ |s| ≤ 100

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    

    #Function to find the maximum occurring character in a string.

    def getMaxOccurringChar(self,s):

        #code here

Solutions:
class Solution:

	def getMaxOccurringChar(self, s):
		t = list(set(list(s)))
		f = []
		for i in range(len(t)):
			f.append(0)
		for i in s:
			f[t.index(i)] += 1
		l = []
		m = max(f)
		for i in range(len(f)):
			if f[i] == m:
				l.append(t[i])
		return min(l)


class Solution:

	def getMaxOccurringChar(self, s):
		s = sorted(s)
		return sorted(s, key=lambda x: s.count(x), reverse=True)[0]


class Solution:

	def getMaxOccurringChar(self, s):
		l = list(s)
		l.sort()
		count = 0
		s1 = ''
		for i in l:
			if l.count(i) > count:
				count = l.count(i)
				s1 = i
		return s1


class Solution:

	def getMaxOccurringChar(self, s):
		l = list(s)
		maxi = 0
		result = ''
		for i in l:
			d = l.count(i)
			if d > maxi:
				maxi = d
				result = i
			elif d == maxi:
				if i < result:
					result = i
		return result


class Solution:

	def getMaxOccurringChar(self, s):
		s = list(s)
		s = sorted(s)
		d = dict()
		for i in s:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		m = max(d.values())
		for i in sorted(d):
			if d[i] == m:
				return i


from collections import Counter

class Solution:

	def getMaxOccurringChar(self, s):
		a = []
		t = Counter(s)
		m = max(t.values())
		for i in t:
			if t[i] == m:
				a.append(i)
		return min(a)


from collections import defaultdict

class Solution:

	def getMaxOccurringChar(self, s):
		chrs = [0] * 256
		res = 0
		ch = chr(265)
		for x in s:
			chrs[ord(x)] += 1
			if chrs[ord(x)] >= res:
				if chrs[ord(x)] > res:
					res += 1
					ch = x
					continue
				ch = min(ch, x)
		return ch


class Solution:

	def getMaxOccurringChar(self, s):
		dic = {}
		for i in s:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		m = max(dic.values())
		for i in sorted(dic):
			if dic[i] == m:
				return i


class Solution:

	def getMaxOccurringChar(self, s):
		count = [0] * 256
		for i in s:
			count[ord(i)] += 1
		max = -1
		res = ' '
		for i in s:
			if count[ord(i)] > max:
				max = count[ord(i)]
				res = i
			elif count[ord(i)] == max and res > i:
				res = i
		return res


class Solution:

	def getMaxOccurringChar(self, s):
		c = []
		a = sorted(s)
		for i in a:
			c.append(a.count(i))
		m = max(c)
		for j in a:
			if a.count(j) == m:
				return j
				break
		else:
			return -1


class Solution:

	def getMaxOccurringChar(self, s):
		freq = {}
		max_freq = 0
		max_char = ''
		for c in s:
			freq[c] = freq.get(c, 0) + 1
		for c in freq:
			if freq[c] > max_freq:
				max_freq = freq[c]
				max_char = c
			elif freq[c] == max_freq and c < max_char:
				max_char = c
		return max_char


class Solution:

	def getMaxOccurringChar(self, s):
		d = [0] * 256
		for c in s:
			if d[ord(c)] == 0:
				d[ord(c)] = 1
			else:
				d[ord(c)] = d[ord(c)] + 1
		res = 0
		char = 0
		for i in range(len(d)):
			count = d[i]
			if count > res:
				res = count
				char = i
		return chr(char)


class Solution:

	def getMaxOccurringChar(self, s):
		char_count = {}
		for ch in s:
			if ch in char_count:
				char_count[ch] += 1
			else:
				char_count[ch] = 1
		max_count = max(char_count.values())
		max_chars = [ch for (ch, count) in char_count.items() if count == max_count]
		max_chars.sort()
		return max_chars[0]


class Solution:

	def getMaxOccurringChar(self, s):
		c = [0] * 26
		for i in range(len(s)):
			temp = ord(s[i]) - 97
			c[temp] += 1
		max1 = c[0]
		l = 0
		for i in range(26):
			if c[i] > max1:
				max1 = c[i]
				l = i
		return chr(l + 97)


class Solution:

	def getMaxOccurringChar(self, s):
		maxx = 0
		alpha = 0
		m = []
		k = list(s)
		for i in k:
			if k.count(i) > maxx:
				maxx = k.count(i)
				alpha = i
				m.clear()
				m.append(i)
			elif k.count(i) == maxx:
				m.append(i)
		return min(m)


class Solution:

	def getMaxOccurringChar(self, s):
		freq = {}
		res = ''
		for i in s:
			freq[i] = freq.get(i, 0) + 1
		for i in freq.keys():
			if freq[i] == max(freq.values()):
				res += i
		res = sorted(res)
		return res[0]


class Solution:

	def getMaxOccurringChar(self, s):
		counter = {}
		max_chars = ''
		for i in s:
			if i in counter:
				counter[i] += 1
			else:
				counter[i] = 1
		for i in counter.keys():
			if counter[i] == max(counter.values()):
				max_chars += i
		max_chars = sorted(max_chars)
		return max_chars[0]


class Solution:

	def getMaxOccurringChar(self, s):
		li = []
		for i in range(26):
			li.append(0)
		for i in s:
			li[ord(i) - 97] += 1
		maxx = max(li)
		for i in range(len(li)):
			if li[i] == maxx:
				return chr(i + 97)


class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		mo = 0
		moc = ''
		for i in s:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			if mo < d[i]:
				mo = d[i]
				moc = i
			elif mo == d[i]:
				if ord(moc) > ord(i):
					moc = i
		return moc


class Solution:

	def getMaxOccurringChar(self, s):
		countm = 0
		for i in s:
			cnt = s.count(i)
			if cnt > countm:
				countm = cnt
				maxi = i
			elif cnt == countm and i < maxi:
				maxi = i
		return maxi


class Solution:

	def getMaxOccurringChar(self, s):
		count = [0] * 26
		maxv = -float('inf')
		for i in s:
			count[ord(i.lower()) - ord('a')] += 1
			maxv = max(maxv, count[ord(i.lower()) - ord('a')])
		for i in range(26):
			if count[i] == maxv:
				return chr(97 + i)


class Solution:

	def getMaxOccurringChar(self, s):
		dic = {}
		for i in s:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		maxx = 0
		for i in dic:
			if dic[i] > maxx:
				maxx = dic[i]
				maxi = i
			elif dic[i] == maxx and i < maxi:
				maxi = i
		return maxi


class Solution:

	def getMaxOccurringChar(self, s):
		dict1 = {}
		for i in range(len(s)):
			dict1.update({s[i]: s.count(s[i])})
		m = max(dict1.values())
		for i in sorted(dict1):
			if dict1[i] == m:
				return i


from collections import Counter

class Solution:

	def getMaxOccurringChar(self, s):
		l = []
		temp = Counter(s)
		maxi = max(temp.values())
		for i in temp:
			if temp[i] == maxi:
				l.append(i)
		return min(l)


class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		l = []
		for i in s:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		h = max(d.values())
		for (k, v) in d.items():
			if h == v:
				l.append(k)
		l.sort()
		w = ''.join(l)
		return w[0]


class Solution:

	def getMaxOccurringChar(self, s):
		lexi = [0] * 256
		for i in range(len(s)):
			lexi[ord(s[i])] = lexi[ord(s[i])] + 1
		return chr(lexi.index(max(lexi)))


class Solution:

	def getMaxOccurringChar(self, s):
		map = {}
		for i in range(len(s)):
			if s[i] in map:
				map[s[i]] += 1
			else:
				map[s[i]] = 1
		maxFreq = 0
		ans = ''
		for (x, y) in map.items():
			if y == maxFreq:
				if x < ans:
					ans = x
			if y > maxFreq:
				maxFreq = y
				ans = x
		return ans


class Solution:

	def getMaxOccurringChar(self, s):
		words = list(s)
		count = []
		m = 0
		al = 0
		for i in words:
			if words.count(i) > m:
				m = words.count(i)
				al = i
				count.clear()
				count.append(i)
			elif words.count(i) == m:
				count.append(i)
		return min(count)


class Solution:

	def getMaxOccurringChar(self, s):
		mf = {}
		ans = []
		count = 0
		n = len(s)
		for i in range(n):
			if s[i] in mf:
				mf[s[i]] += 1
			else:
				mf[s[i]] = 1
		count = max(list(mf.values()))
		for (i, k) in mf.items():
			if count == k:
				ans.append(i)
		return min(ans)


class Solution:

	def getMaxOccurringChar(self, s):
		maxi = 0
		ans = []
		d = list(s)
		for i in d:
			if d.count(i) > maxi:
				maxi = d.count(i)
				ans.clear()
				ans.append(i)
			elif d.count(i) == maxi:
				ans.append(i)
		return min(ans)


class Solution:

	def getMaxOccurringChar(self, s):
		l1 = []
		maximum = 0
		for i in s:
			if s.count(i) > maximum:
				maximum = s.count(i)
				l1.clear()
				l1.append(i)
			elif s.count(i) == maximum:
				l1.append(i)
		return min(l1)


import operator

class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		for i in s:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		a = [[i, d[i]] for i in d]
		v = max(d.values())
		dummy = []
		for i in d:
			if d[i] == v:
				dummy.append(i)
		dummy.sort()
		return dummy[0]


class Solution:

	def getMaxOccurringChar(self, s):
		dic = {}
		for i in s:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		maxi = max(dic.values())
		dic = sorted(dic.items(), key=lambda x: x[0])
		for (i, v) in dic:
			if v == maxi:
				return i


class Solution:

	def getMaxOccurringChar(self, s):
		max = 0
		alpha = 0
		k = list(s)
		temp = []
		for i in k:
			if k.count(i) > max:
				max = k.count(i)
				alpha = i
				temp.clear()
				temp.append(i)
			elif k.count(i) == max:
				temp.append(i)
		return min(temp)


class Solution:

	def getMaxOccurringChar(self, s):
		d = dict()
		for i in s:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		maxx = 0
		for x in d.values():
			if maxx < x:
				maxx = x
		l = 'z'
		for i in d:
			if d[i] == maxx and l > i:
				l = i
		return l


class Solution:

	def getMaxOccurringChar(self, s):
		s = list(s.rstrip())
		p = list(set(s))
		d = []
		for i in range(len(p)):
			d.append(s.count(p[i]))
		f = []
		g = max(d)
		for i in range(len(p)):
			if d[i] == g:
				f.append(p[i])
		f = sorted(f)
		return f[0]


class Solution:

	def getMaxOccurringChar(self, s):
		dic = {}
		for i in s:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		maxi = max(dic.values())
		a = []
		for (i, j) in dic.items():
			if j == maxi:
				a.append(i)
		a.sort()
		return a[0]


class Solution:

	def getMaxOccurringChar(self, s):
		s = sorted(list(s))
		n = len(s)
		(m, mm, l) = (1, 0, '')
		for i in range(1, n):
			if s[i] == s[i - 1]:
				m += 1
			else:
				if mm < m:
					mm = m
					l = s[i - 1]
				m = 1
		if mm < m:
			try:
				l = s[i]
			except:
				return s[0]
		return l


class Solution:

	def getMaxOccurringChar(self, s):
		unique_chars = sorted(set(s))
		(max_count, max_char) = (s.count(unique_chars[0]), unique_chars[0])
		for i in range(1, len(unique_chars)):
			if s.count(unique_chars[i]) > max_count:
				(max_count, max_char) = (s.count(unique_chars[i]), unique_chars[i])
		return max_char


class Solution:

	def getMaxOccurringChar(self, s):
		st = list(set(s))
		li = []
		for i in range(len(st)):
			c = s.count(st[i])
			li.append(c)
		m = max(li)
		li1 = []
		for j in range(len(li)):
			if m == li[j]:
				li1.append(st[j])
		li1.sort()
		return li1[0]


from collections import Counter

class Solution:

	def getMaxOccurringChar(self, s):
		maxx = 0
		alpha = 0
		m = []
		k = list(s)
		for i in k:
			if k.count(i) > maxx:
				maxx = k.count(i)
				alpha = i
				m.clear()
				m.append(i)
			elif k.count(i) == maxx:
				m.append(i)
		return min(m)


class Solution:

	def getMaxOccurringChar(self, s):
		arr = [0] * 26
		c = 0
		res = 0
		for i in range(len(s)):
			arr[ord(s[i]) - ord('a')] += 1
		for i in range(len(arr)):
			if arr[i] == max(arr):
				c = i
				break
		return chr(97 + c)


class Solution:

	def getMaxOccurringChar(self, s):
		s = sorted(s)
		maxx = s[0]
		for i in s[1:]:
			if s.count(i) > s.count(maxx):
				maxx = i
		return maxx


class Solution:

	def getMaxOccurringChar(self, s):
		dict = {}
		for i in s:
			if i in dict:
				dict[i] += 1
			else:
				dict[i] = 1
		a = [i for i in dict if dict[i] == max(dict.values())]
		a.sort()
		return a[0]


class Solution:

	def getMaxOccurringChar(self, s):
		moc = {}
		for ch in s:
			moc[ch] = moc.get(ch, 0) + 1
		ans = 'z'
		max_count = 0
		for ch in set(s):
			if moc[ch] > max_count:
				ans = ch
				max_count = moc[ch]
			elif moc[ch] == max_count and ans > ch:
				ans = ch
		return ans


class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		for i in s:
			d[i] = d.get(i, 0) + 1
		maxValue = 0
		ch = 123
		for (key, value) in d.items():
			if value >= maxValue:
				maxValue = value
		for (key, value) in d.items():
			if value == maxValue and ord(key) < ch:
				ch = ord(key)
		return chr(ch)


class Solution:

	def getMaxOccurringChar(self, s):
		s = sorted(s)
		dict = {}
		for i in s:
			if i in dict:
				dict[i] += 1
			else:
				dict[i] = 1
		maxEle = s[0]
		maxPos = dict[s[0]]
		for i in s:
			if dict[i] > maxPos:
				maxPos = dict[i]
				maxEle = i
		return maxEle


class Solution:

	def getMaxOccurringChar(self, s):
		a = [0] * 26
		for i in s:
			a[ord(i) - 97] += 1
		maxm = 0
		l = 0
		for i in range(26):
			if maxm < a[i]:
				l = i
				maxm = a[i]
		return chr(l + 97)


class Solution:

	def getMaxOccurringChar(self, s):
		count = 0
		a = []
		for i in s:
			if count < s.count(i):
				a.clear()
				count = s.count(i)
				a.append(i)
			if count == s.count(i):
				a.append(i)
		a.sort()
		return a[0]


class Solution:

	def getMaxOccurringChar(self, s):
		dic = {}
		for i in s:
			dic[i] = s.count(i)
		max_count = max(dic.values())
		char = ''
		for (key, val) in dic.items():
			if val == max_count:
				if char == '' or key < char:
					char = key
		return char


class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		for i in s:
			d.update({i: s.count(i)})
		max = -1
		b = []
		for i in d.values():
			if i > max:
				max = i
		for i in d.keys():
			if d[i] == max:
				b.append(i)
		return min(b)


class Solution:

	def getMaxOccurringChar(self, s):
		D = {}
		for i in s:
			if i not in D:
				D[i] = 1
			else:
				X = D.get(i)
				D[i] = X + 1
		Max = list(D.values())
		Max.sort()
		Mvalues = Max[-1]
		Y = {i for i in D if D[i] == Mvalues}
		AAA = list(Y)
		AAA.sort()
		ans = AAA[0]
		return ans


class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		for i in s:
			if i in d:
				d[i] += int(1)
			else:
				d[i] = int(1)
		max = 0
		arr = []
		for key in d:
			if d[key] > max:
				max = d[key]
		newmax = max
		arr = []
		for i in d:
			if d[i] == max:
				arr.append(i)
		arr.sort()
		return arr[0]


class Solution:

	def getMaxOccurringChar(self, s):
		freq = [0] * 26
		m = 0
		mi = 'z'
		for i in s:
			freq[ord(i) - ord('a')] += 1
			if m < freq[ord(i) - ord('a')]:
				m = freq[ord(i) - ord('a')]
				mi = i
			elif m == freq[ord(i) - ord('a')] and mi > i:
				mi = i
		return mi


class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		for i in range(len(s)):
			if s[i] not in d:
				d[s[i]] = 1
			else:
				d[s[i]] += 1
		max1 = max(d.values())
		l = []
		for (x, y) in d.items():
			if y == max1:
				l.append(x)
		return min(l)


class Solution:

	def getMaxOccurringChar(self, s):
		dic = {}
		for i in s:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		max_char = max(dic, key=dic.get)
		l = [max_char]
		for i in dic:
			if dic[i] == dic[max_char] and max_char != i:
				l.append(i)
		return sorted(l)[0]


class Solution:

	def getMaxOccurringChar(self, s):
		freq = {}
		for char in s:
			if char in freq:
				freq[char] += 1
			else:
				freq[char] = 1
		max_freq = max(freq.values())
		max_char = min([char for char in freq if freq[char] == max_freq])
		return max_char


class Solution:

	def getMaxOccurringChar(self, s):
		max = s[0]
		for i in range(0, len(s)):
			if s.count(s[i]) > s.count(max):
				max = s[i]
			if s[i] < max and s.count(s[i]) >= s.count(max):
				max = s[i]
		return max


class Solution:

	def getMaxOccurringChar(self, s):
		dict = {}
		for i in range(len(s)):
			if s[i] in dict:
				dict[s[i]] = dict[s[i]] + 1
			else:
				dict[s[i]] = 1
		max_value = max(dict.values())
		for i in sorted(dict.keys()):
			if dict[i] == max_value:
				return i


class Solution:

	def getMaxOccurringChar(self, s):
		m_d = {}
		for i in range(0, len(s)):
			char = s[i]
			if char in m_d:
				m_d[char] = m_d[char] + 1
			else:
				m_d[char] = 1
		max_val = max(m_d.values())
		for i in sorted(m_d.keys()):
			if m_d[i] == max_val:
				return i


class Solution:

	def getMaxOccurringChar(self, s):
		temp = set(s)
		tempNew = list(temp)
		tempNew.sort()
		count = 0
		ans = 0
		for i in tempNew:
			if s.count(i) > count:
				ans = i
				count = s.count(i)
		return ans


class Solution:

	def getMaxOccurringChar(self, s):
		str1 = set(s)
		lst = sorted(list(str1))
		l1 = []
		l2 = []
		for i in lst:
			cnt = s.count(i)
			l1.append(cnt)
		res = max(l1)
		for i in lst:
			if s.count(i) == res:
				l2.append(i)
				a = l2[0]
		return a


class Solution:

	def getMaxOccurringChar(self, s):
		l = []
		l1 = []
		for i in s:
			l.append(s.count(i))
		m = max(l)
		for i in range(len(l)):
			if l[i] == m:
				l1.append(s[i])
		l1 = list(set(l1))
		if len(l1) >= 2:
			l1.sort()
			return l1[0]
		else:
			return l1[0]


class Solution:

	def getMaxOccurringChar(self, s):
		l = []
		for i in range(len(s)):
			l.append(s.count(s[i]))
		m = max(l)
		l1 = set()
		for i in range(len(l)):
			if m == l[i]:
				l1.add(s[i])
		return min(l1)


class Solution:

	def getMaxOccurringChar(self, s):
		char_dict = {}
		for each_c in s:
			if each_c in char_dict.keys():
				char_dict[each_c] += 1
			else:
				char_dict[each_c] = 1
		c_list = []
		max_occr = max(char_dict.values())
		for (c, v) in char_dict.items():
			if v == max_occr:
				c_list.append(c)
		return min(c_list)


import collections

class Solution:

	def getMaxOccurringChar(self, s):
		d = [0] * 26
		for i in range(len(s)):
			d[ord(s[i]) - 97] += 1
		m = max(d)
		for i in range(len(d)):
			if d[i] == m:
				return chr(i + 97)


class Solution:

	def getMaxOccurringChar(self, s):
		d = {}
		for i in s:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		m = s[0]
		for j in d:
			if d[j] > d[m]:
				m = j
			elif d[j] == d[m] and j < m:
				m = j
		return m