Given an array, the task is to find the difference between highest occurrence and lowest occurrence of any number in an array.
Note: If only one type of element is present in the array return 0.
 
Example 1:
Input:
arr[] = [1, 2, 2]
Output:
1
Explanation:
Lowest occurring element (1) occurs once.
Highest occurring element (2) occurs 2 times
 
Example 2:
Input : 
arr[] = [7, 8, 4, 5, 4, 1, 1, 7, 7, 2, 5]
Output : 
2
Explanation :
Lowest occurring element (2) occurs once.
Highest occurring element (7) occurs 3 times
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array arr[] and its size N as inputs and returns the difference between highest occurrence and lowest occurrence of any number in the array.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1<=N<=10^{5}
1<=A[i]<=10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def findDiff(self, a, n):

        #Code here

    

    

    

    

Solutions:
class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		return max(d.values()) - min(d.values())


class Solution:

	def findDiff(self, arr, n):
		d = {}
		for i in range(n):
			if arr[i] not in d:
				d[arr[i]] = 1
			else:
				d[arr[i]] += 1
		l = d.values()
		if max(l) - min(l) > 0:
			return max(l) - min(l)
		else:
			return 0


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		min1 = 99999
		max1 = -1
		for i in d:
			if d[i] < min1:
				min1 = d[i]
			if d[i] > max1:
				max1 = d[i]
		if max1 != -1 and min1 != 99999:
			return max1 - min1
		else:
			return 0


class Solution:

	def findDiff(self, a, n):
		b = {}
		for i in range(n):
			b[a[i]] = b.get(a[i], 0) + 1
		low = min(b.values())
		high = max(b.values())
		if len(b) == 1:
			return 0
		return abs(high - low)


import collections

class Solution:

	def findDiff(self, a, n):
		m = collections.Counter(a)
		return max(m.values()) - min(m.values())


class Solution:

	def findDiff(self, a, n):
		d = dict()
		for ele in a:
			d[ele] = 0
		for ele in a:
			d[ele] = d[ele] + 1
		mini = 99999999999999
		maxi = 0
		for ele in d.keys():
			if d[ele] > maxi:
				maxi = d[ele]
			if d[ele] < mini:
				mini = d[ele]
		return maxi - mini


class Solution:

	def findDiff(self, a, n):
		d = dict()
		for ele in a:
			d[ele] = 0
		for ele in a:
			d[ele] = d[ele] + 1
		arr = []
		for ele in d.keys():
			arr.append(d[ele])
		return max(arr) - min(arr)


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		b = set(a)
		if len(b) == 1 or n == 1:
			return 0
		else:
			d = dict(Counter(a))
			m = max(d.values())
			mi = min(d.values())
		return abs(m - mi)


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		c = Counter(a)
		return c.most_common()[0][1] - c.most_common()[-1][1]


class Solution:

	def findDiff(self, a, n):
		dict_ = {}
		for elem in a:
			dict_.update({elem: dict_.get(elem, 0) + 1})
		val = list(dict_.values())
		return max(val) - min(val)


class Solution:

	def findDiff(self, a, n):
		freq = {}
		for i in a:
			if i not in freq:
				freq[i] = 0
			freq[i] += 1
		less = 100000000.0
		more = -100000000.0
		for i in freq:
			if freq[i] < less:
				less = freq[i]
			if freq[i] > more:
				more = freq[i]
		return more - less


class Solution:

	def findDiff(self, a, n):
		max1 = 0
		for i in a:
			if int(i) > max1:
				max1 = int(i)
		occ = [0 for x in range(max1)]
		for i in range(len(a)):
			occ[int(a[i]) - 1] += 1
		min1 = max(occ)
		for i in occ:
			if i < min1 and i > 0:
				min1 = i
		if max(occ) != min(occ):
			return max(occ) - min1
		else:
			return 0


from collections import *

class Solution:

	def findDiff(self, a, n):
		c = Counter(a)
		return max(c.values()) - min(c.values())


class Solution:

	def findDiff(self, a, n):
		s = {}
		for i in a:
			if i in s:
				s[i] += 1
			else:
				s[i] = 1
		b = []
		for i in s:
			b.append(s[i])
		b.sort()
		return max(b) - min(b)


class Solution:

	def findDiff(self, a, n):
		se = set(a)
		if len(se) == 1:
			return 0
		d = {}
		for i in a:
			d[i] = d.get(i, 0) + 1
		highest = 1
		lowest = min(d.values())
		for i in d:
			if d[i] > highest:
				highest = d[i]
			if d[i] < lowest:
				lowest = d[i]
		return highest - lowest


from collections import defaultdict

class Solution:

	def findDiff(self, a, n):
		d = defaultdict()
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		dv = list(d.values())
		min_v = min(dv)
		max_v = max(dv)
		return max_v - min_v


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in a:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		dv = list(d.values())
		dvma = max(dv)
		dvmi = min(dv)
		return dvma - dvmi


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		d = Counter(a)
		maxi = 0
		mini = n
		for (key, values) in d.items():
			maxi = max(maxi, values)
			mini = min(mini, values)
		return maxi - mini


class Solution:

	def findDiff(self, a, n):
		freq = {}
		for i in a:
			if i in freq:
				freq[i] += 1
			else:
				freq[i] = 1
		val = []
		for (k, v) in freq.items():
			val.append(v)
		return max(val) - min(val)


class Solution:

	def findDiff(self, a, n):
		dic = {}
		for i in a:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		val = dic.values()
		ho = max(val)
		so = min(val)
		res = ho - so
		return res


class Solution:

	def findDiff(self, a, n):
		M = 0
		for i in a:
			if int(i) > M:
				M = int(i)
		lst = [0] * (M + 1)
		for i in a:
			lst[int(i)] += 1
		Min = 0
		flag = 0
		for i in lst:
			if i != 0 and flag == 0:
				Min = i
				flag = 1
			elif i != 0 and i < Min:
				Min = i
		flag = 0
		Max = 0
		for i in lst:
			if i > Max:
				Max = i
		return Max - Min


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in a:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		if len(d) == 1:
			return 0
		ma = 0
		for j in a:
			if d[j] > ma:
				ma = d[j]
		mi = 99999
		for i in a:
			if d[i] < mi:
				mi = d[i]
		return ma - mi


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		freq = Counter(a)
		(mx, mn) = (-1, 1000)
		for (i, j) in freq.items():
			mx = max(mx, j)
			mn = min(mn, j)
		return mx - mn


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		arr = Counter(a)
		occur_list = [value for (key, value) in arr.items()]
		highest = max(occur_list)
		lowest = min(occur_list)
		return highest - lowest


class Solution:

	def findDiff(self, a, n):
		a2 = [int(j) for j in a]
		d = {}
		for j in a:
			if j in d:
				d[j] += 1
			else:
				d[j] = 0
		l = list(d.values())
		return max(l) - min(l)


class Solution:

	def findDiff(self, a, n):
		minkey = 0
		maxkey = 0
		mino = 0
		maxo = 0
		H = {}
		for i in range(n):
			H[int(a[i])] = H.get(int(a[i]), 0) + 1
		(minkey, maxkey) = (min(H.values()), max(H.values()))
		if minkey == maxkey:
			return 0
		for k in H.keys():
			if H[k] == minkey:
				mino = k
			if H[k] == maxkey:
				maxo = k
		return maxkey - minkey


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in range(n):
			if a[i] in d:
				d[a[i]] += 1
			else:
				d[a[i]] = 1
		high = max(d, key=d.get)
		low = min(d, key=d.get)
		return abs(d[high] - d[low])


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		l = []
		d = Counter(a)
		for i in d.values():
			l.append(i)
		a = max(l) - min(l)
		return a


class Solution:

	def findDiff(self, a, n):
		dic = {}
		for i in a:
			if i in dic:
				dic[i] = dic[i] + 1
			if i not in dic:
				dic[i] = 1
		mini = 100001
		maxi = 0
		for (key, value) in dic.items():
			if mini > value:
				mini = value
				min_key = key
		for (key, value) in dic.items():
			if maxi < value:
				maxi = value
				max_key = key
		if len(dic) == 1:
			return 0
		else:
			return int(maxi) - int(mini)


class Solution:

	def findDiff(self, a, n):
		h = {}
		for i in a:
			if i not in h:
				h[i] = 1
				continue
			h[i] += 1
		return max(h.values()) - min(h.values())


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		min1 = d[a[n - 1]]
		max1 = 0
		for x in d.values():
			min1 = min(min1, x)
			max1 = max(max1, x)
		return max1 - min1


class Solution:

	def findDiff(self, a, n):
		hashMap = {}
		for i in range(n):
			hashMap[a[i]] = 1 + hashMap.get(a[i], 0)
		maxEle = float('-inf')
		minEle = float('inf')
		for i in hashMap.values():
			currEle = i
			maxEle = max(maxEle, i)
			minEle = min(minEle, i)
		return maxEle - minEle


import sys

class Solution:

	def findDiff(self, a, n):
		d = {}
		if len(a) <= 1:
			return 0
		for i in a:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		maxi = -sys.maxsize - 1
		mini = sys.maxsize
		for i in d.values():
			maxi = max(i, maxi)
			mini = min(i, mini)
		return maxi - mini if maxi > mini else mini - maxi


class Solution:

	def findDiff(self, a, n):
		freqmap = {}
		for i in a:
			if freqmap.get(i):
				freqmap[i] += 1
			else:
				freqmap[i] = 1
		l = min(freqmap.values())
		h = max(freqmap.values())
		return h - l


class Solution:

	def findDiff(self, a, n):
		h = {}
		for i in a:
			h[i] = 1 + h.get(i, 0)
		mini = 99999
		maxi = -99999
		for i in h:
			mini = min(mini, h[i])
			maxi = max(maxi, h[i])
		return maxi - mini


class Solution:

	def findDiff(self, a, n):
		s = set()
		d = {}
		for i in a:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for v in d.values():
			s.add(v)
		if len(s) == 1:
			return 0
		else:
			s = list(s)
			s.sort()
			x = s[-1] - s[0]
			return x


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in a:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		m = sorted(d.values())
		if len(d) > 0:
			return max(m) - min(m)


class Solution:

	def findDiff(self, a, n):
		dict1 = {}
		for i in range(n):
			dict1[a[i]] = dict1.get(a[i], 0) + 1
		if len(dict1) == 1:
			return 0
		mini = min(dict1.values())
		maxi = max(dict1.values())
		return maxi - mini


class Solution:

	def findDiff(self, a, n):
		hm = {}
		for elem in a:
			hm[elem] = hm.get(elem, 0) + 1
		max1 = max(hm.values())
		min1 = min(hm.values())
		return max1 - min1


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		l = []
		m = Counter(a)
		for (keys, values) in m.items():
			l.append(values)
		l.sort()
		if len(l) == 1:
			return 0
		else:
			return l[len(l) - 1] - l[0]


class Solution:

	def findDiff(self, a, n):
		d = {}
		for val in a:
			d[val] = d.get(val, 0) + 1
		count = d.values()
		return max(count) - min(count)


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in range(n):
			if a[i] in d:
				d[a[i]] += 1
			else:
				d[a[i]] = 1
		(maxc, minc) = (0, n)
		for (k, v) in d.items():
			maxc = max(maxc, v)
			minc = min(minc, v)
		return maxc - minc


class Solution:

	def findDiff(self, a, n):
		if len(a) < 2:
			return 0
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		a.sort()
		mi = d[a[0]]
		ma = d[a[1]]
		for i in range(0, n):
			if d[a[i]] < mi:
				mi = d[a[i]]
			elif d[a[i]] > ma:
				ma = d[a[i]]
		return ma - mi


class Solution:

	def findDiff(self, a, n):
		d = {}
		(small, big) = (a[0], a[0])
		(cs, cr) = (0, 0)
		if n == 1:
			return 0
		else:
			for i in range(n):
				if a[i] in d:
					d[a[i]] += 1
				else:
					d[a[i]] = 1
			for (key, value) in d.items():
				if d[key] <= d[small]:
					small = key
					cs = int(d[key])
				if d[key] >= d[big]:
					big = key
					cr = int(d[key])
		diff = cr - cs
		return diff


class Solution:

	def findDiff(self, a, n):
		k = {}
		for i in a:
			if i in k:
				k[i] += 1
			else:
				k[i] = 1
		b = []
		for j in k:
			b.append(k[j])
		return max(b) - min(b)


class Solution:

	def findDiff(self, a, n):
		di = {}
		for i in a:
			di[i] = di.get(i, 0) + 1
		return max(di.values()) - min(di.values())


from collections import Counter

class Solution:

	def findDiff(self, a, n):
		d1 = Counter(a)
		if len(d1) == 1:
			return 0
		highest = max(d1.values())
		lowest = min(d1.values())
		return highest - lowest


class Solution:

	def findDiff(self, a, n):
		s = {}
		for i in a:
			if i not in s:
				s[i] = 1
			else:
				s[i] += 1
		m = max(s.values())
		for i in s:
			if s[i] == m:
				break
		n = min(s.values())
		for i in s:
			if s[i] == n:
				return abs(m - n)


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in range(n):
			if a[i] in d:
				d[a[i]] += 1
			else:
				d[a[i]] = 1
		val = []
		for i in d.values():
			val.append(i)
		val.sort()
		return val[-1] - val[0]


class Solution:

	def findDiff(self, a, n):
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		k = 0
		l = 0
		s = min(d.values())
		f = max(d.values())
		for (i, j) in d.items():
			if d[i] == s:
				k = d[i]
			if d[i] == f:
				l = d[i]
		return l - k