Given an array A of size N. The elements of the array consist of positive integers. You have to find the largest element with minimum frequency. 
Example 1:
Input: 
5
2 2 5 50 1
Output:
50
Explanation :
All elements are having frequency 1 except 2.
50 is the maximum element with minimum frequency.
Example 2:
Input:
4
3 3 5 5
Output:
5
Explanation:
Both 3 and 5 have the same frequency, so 5 should be returned.
User Task:
Your task is to complete the provided function LargButMinFreq(A, n) which accepts array A and n. Hence you have to return the largest element with minimum frequency.
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3





def LargButMinFreq(arr,n):

    #code here

Solutions:
def LargButMinFreq(arr, n):
	d = {}
	mini = 9999999
	maxi = 0
	for i in arr:
		d[i] = 0
	for i in range(n):
		d[arr[i]] += 1
	for (i, j) in d.items():
		mini = min(mini, j)
	for (i, j) in d.items():
		if mini == j:
			maxi = max(maxi, i)
	return maxi


from collections import Counter

def LargButMinFreq(arr, n):
	p = sorted(arr)
	c = Counter(p)
	q = min(c.values())
	for i in p[::-1]:
		if c[i] == q:
			return i


from collections import OrderedDict

def LargButMinFreq(arr, n):
	arr.sort()
	arr = arr[::-1]
	dic = OrderedDict()
	for e in arr:
		try:
			c = dic[e]
			dic[e] = c + 1
		except:
			dic[e] = 1
	minn = 1000000000
	maxx = 0
	for (i, v) in dic.items():
		if v < minn:
			minn = v
	for (i, v) in dic.items():
		if v == minn:
			if i > maxx:
				maxx = i
	return maxx


def LargButMinFreq(arr, n):
	frequency_table = {}
	for element in arr:
		if element in frequency_table:
			frequency_table[element] += 1
		else:
			frequency_table[element] = 1
	min_frequency = float('inf')
	for (number, frequency) in frequency_table.items():
		if frequency < min_frequency or (frequency == min_frequency and number > largest_number):
			min_frequency = frequency
			largest_number = number
	return largest_number


def LargButMinFreq(arr, n):
	dic = {}
	lst = []
	for i in arr:
		dic[i] = 0
	for i in arr:
		dic[i] = dic[i] + 1
	for i in dic.keys():
		lst.append(dic[i])
	m = min(lst)
	lst1 = []
	for i in dic.keys():
		if dic[i] == m:
			lst1.append(i)
	return max(lst1)


def LargButMinFreq(arr, n):
	obj = {}
	for x in arr:
		if obj.get(x) is None:
			obj[x] = 1
		else:
			obj[x] += 1
	minFreq = min(obj.values())
	max = 0
	for (x, y) in obj.items():
		if y == minFreq and x > max:
			max = x
	return max


import math

def LargButMinFreq(arr, n):
	maxi = -math.inf
	mini = math.inf
	dicti = {}
	for i in arr:
		if i not in dicti:
			dicti[i] = 1
		else:
			dicti[i] += 1
	mini = min([value for (key, value) in dicti.items()])
	for i in dicti:
		if dicti[i] == mini:
			maxi = max(maxi, i)
	return maxi


def LargButMinFreq(arr, n):
	d = {}
	arr.sort()
	k = arr[::-1]
	lst = []
	l = []
	for i in arr:
		d[i] = 0
	for i in arr:
		d[i] = d[i] + 1
	for i in d:
		lst.append(d[i])
	t = min(lst)
	for i in d:
		if d[i] == t:
			l.append(i)
	return max(l)


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	a1 = []
	for i in d.keys():
		a1.append(d[i])
	b = min(a1)
	a2 = []
	for i in d:
		if d[i] == b:
			a2.append(i)
	return max(a2)


def LargButMinFreq(arr, n):
	maxFreq = 1
	maxValwithMinFreq = 0
	dict = {}
	for i in arr:
		if dict.get(i) == None:
			dict.update({i: 1})
		else:
			dict.update({i: dict.get(i) + 1})
			if dict.get(i) > maxFreq:
				maxFreq = dict.get(i)
	newarr = set([])
	for x in dict:
		newarr.add(dict[x])
	newarr = list(newarr)
	newarr.sort()
	minfreq = newarr[0]
	maxNum = 0
	for x in dict:
		if minfreq == dict[x]:
			if x > maxNum:
				maxNum = x
	return maxNum


from collections import defaultdict as dd

def LargButMinFreq(arr, n):
	store = dd(lambda : 0)
	for item in arr:
		store[item] += 1
	maxi = 0
	mini = min(store.values())
	for item in store:
		if store[item] == mini and item > maxi:
			maxi = item
	return maxi


def LargButMinFreq(arr, n):
	d = {}
	for i in range(n):
		if arr[i] in d:
			d[arr[i]] += 1
		else:
			d[arr[i]] = 1
	l = []
	for i in d.keys():
		l.append(d[i])
	a = min(l)
	l = []
	for i in d.keys():
		if d[i] == a:
			l.append(i)
	return max(l)


def LargButMinFreq(arr, n):
	freq = {}
	for item in arr:
		if item in freq:
			freq[item] += 1
		else:
			freq[item] = 1
	minfreq = n + 1
	res = -1
	for (key, value) in freq.items():
		if minfreq > value:
			res = key
			minfreq = value
		elif minfreq == value:
			if res < key:
				res = key
	return res


def LargButMinFreq(arr, n):
	dic = {}
	for i in arr:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	output = n + 1
	maxm = 0
	for (key, value) in dic.items():
		if output > value:
			output = value
			maxm = key
		elif output == value:
			if maxm < key:
				maxm = key
	return maxm


from collections import defaultdict

def LargButMinFreq(arr, n):
	freq_dict = {}
	for num in arr:
		freq_dict[num] = freq_dict.get(num, 0) + 1
		min_freq = float('inf')
		max_num = float('-inf')
	for (num, freq) in freq_dict.items():
		if freq < min_freq:
			min_freq = freq
			max_num = num
		elif freq == min_freq:
			max_num = max(max_num, num)
	return max_num


def LargButMinFreq(arr, n):
	from collections import Counter
	f = Counter(arr)
	a = []
	mn = min(list(f.values()))
	for i in f:
		if f[i] == mn:
			a.append(i)
	return max(a)


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


def LargButMinFreq(arr, n):
	hmap = {}
	z = []
	for i in arr:
		if i in hmap:
			hmap[i] += 1
		else:
			hmap[i] = 1
	for ele in hmap.keys():
		z.append(hmap[ele])
	a = min(z)
	k = []
	for ele in hmap.keys():
		if hmap[ele] == a:
			k.append(ele)
	return max(k)


def LargButMinFreq(arr, n):
	d = dict()
	z = []
	for ele in arr:
		d[ele] = 0
	for ele in arr:
		d[ele] = d[ele] + 1
	for ele in d.keys():
		z.append(d[ele])
	a = min(z)
	k = []
	for ele in d.keys():
		if d[ele] == a:
			k.append(ele)
	return max(k)


from collections import Counter
import sys

def LargButMinFreq(arr, n):
	d = dict()
	for i in arr:
		if i not in d:
			d[i] = 0
		else:
			d[i] += 1
	minf = min(d.values())
	l = []
	for i in d:
		if d[i] == minf:
			l.append(i)
	return max(l)


def LargButMinFreq(arr, n):
	d = {}
	for i in range(n):
		if arr[i] not in d.keys():
			d[arr[i]] = 1
		else:
			d[arr[i]] += 1
	b = []
	for keys in d:
		b.append(d[keys])
	c = set(b)
	g = min(c)
	o = []
	for keys in d:
		if d[keys] == g:
			o.append(keys)
	return max(o)


def LargButMinFreq(arr, n):
	hash = {}
	min_freq = float('inf')
	for item in arr:
		if item in hash:
			hash[item] += 1
		else:
			hash[item] = 1
	for i in hash:
		if hash[i] < min_freq:
			min_freq = hash[i]
	large = -1
	for i in hash:
		if hash[i] == min_freq:
			if large < i:
				large = i
	return large


def LargButMinFreq(arr, n):
	dic = {}
	for i in arr:
		if i in dic.keys():
			dic[i] += 1
		else:
			dic[i] = 1
	min_freq = min(dic.values())
	return max([j for j in dic.keys() if dic[j] == min_freq])


from collections import Counter

def LargButMinFreq(arr, n):
	freq = Counter(arr)
	mn = min(freq.values())
	lst = [i for (i, j) in freq.items() if j == mn]
	return sorted(lst)[-1]


def LargButMinFreq(arr, n):
	l = []
	d = {}
	for ele in arr:
		d[ele] = 0
	for ele in arr:
		d[ele] = d[ele] + 1
	minn = 10000000000
	for (k, v) in d.items():
		if v <= minn:
			minn = v
	for (k, v) in d.items():
		if v == minn:
			l.append(k)
	return max(l)


from collections import Counter

def LargButMinFreq(arr, n):
	arr.sort(reverse=True)
	a = dict(Counter(arr))
	least_val = min(list(a.values()))
	max_val = max([k for k in a.keys() if a[k] == least_val])
	return max_val


from collections import defaultdict

def LargButMinFreq(arr, n):
	map = dict()
	for a in arr:
		map[a] = 1 if a not in map else map[a] + 1
	map_2 = defaultdict(list)
	min_freq = float('inf')
	for (k, v) in map.items():
		map_2[v].append(k)
		if v < min_freq:
			min_freq = v
	return max(map_2[min_freq])


def LargButMinFreq(arr, n):
	dt = {}
	for i in range(n):
		if arr[i] in dt:
			dt[arr[i]] += 1
		else:
			dt[arr[i]] = 1
	maxi = 0
	min_freq = dt[arr[0]]
	for (key, value) in dt.items():
		if value <= min_freq:
			min_freq = value
			if maxi < key:
				maxi = key
	return maxi


def LargButMinFreq(arr, n):
	dt = {}
	for i in range(n):
		if arr[i] in dt:
			dt[arr[i]] += 1
		else:
			dt[arr[i]] = 1
	l = []
	min_freq = dt[arr[0]]
	for (key, value) in dt.items():
		if value <= min_freq:
			min_freq = value
			l.append(key)
	return max(l)


def LargButMinFreq(arr, n):
	from collections import Counter
	c = Counter(arr)
	minfreq = min(c.values())
	ans = float('-inf')
	for (k, v) in c.items():
		if v == minfreq:
			ans = max(ans, k)
	return ans


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	a = min(d.values())
	b = -1
	for i in d:
		if d[i] == a:
			if i > b:
				b = i
	return b


def LargButMinFreq(arr, n):
	h = {}
	for i in arr:
		if i in h:
			h[i] += 1
		else:
			h[i] = 1
	min = h[arr[0]]
	max = arr[0]
	for j in h:
		if h[j] < min:
			min = h[j]
			max = j
		elif h[j] == min and j > max:
			min = h[j]
			max = j
		else:
			pass
	return max


from collections import defaultdict

def LargButMinFreq(arr, n):
	num_frequency_map = defaultdict(int)
	for num in arr:
		num_frequency_map[num] += 1
	min_frequency = len(arr)
	max_min_freq_number = None
	for (num, frequency) in num_frequency_map.items():
		if frequency < min_frequency:
			min_frequency = frequency
			max_min_freq_number = num
		elif frequency == min_frequency and num > max_min_freq_number:
			max_min_freq_number = num
	return max_min_freq_number


def LargButMinFreq(arr, n):
	freq = {}
	for i in arr:
		freq[i] = freq.get(i, 0) + 1
	res = 0
	minFreq = min(freq.values())
	for i in freq:
		if freq[i] == minFreq and res < i:
			res = i
	return res


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		d[i] = d.get(i, 0) + 1
	values = d.values()
	keys = d.keys()
	least_freq = min(values)
	li = []
	for i in d:
		if d.get(i) == least_freq:
			li.append(i)
	return max(li)


from collections import Counter

def LargButMinFreq(arr, n):
	f = Counter(arr)
	m = f[arr[0]]
	for i in f.elements():
		m = f[i] if m > f[i] else m
	r = []
	for i in f.elements():
		r.append(i) if f[i] == m else r
	return max(r)
	return n


def LargButMinFreq(arr, n):
	d = {}
	ans = []
	output = []
	for i in arr:
		if i in d:
			d[i] = d[i] + 1
		else:
			d[i] = 1
	for i in d:
		ans.append(d[i])
	m = min(ans)
	for i in d:
		if d[i] == m:
			output.append(i)
	return max(output)


def LargButMinFreq(arr, n):
	ma = find_max(arr)
	c = [0 for i in range(ma)]
	for i in arr:
		occr = c[i - 1] + 1
		c[i - 1] = occr
	mi = find_min(c)
	for i in range(len(c) - 1, -1, -1):
		if c[i] == mi:
			return i + 1

def find_max(arr):
	m = None
	for i in arr:
		if m == None or m < i:
			m = i
	return m

def find_min(arr):
	m = None
	for i in arr:
		if m == None or m > i:
			if i > 0:
				m = i
	return m


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		d[i] = d.get(i, 0) + 1
	x = d.keys()
	y = min(d.values())
	l = []
	for i in d:
		if d[i] == y:
			l.append(i)
	return max(l)


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	temp = min(d.values())
	l = [key for key in d if d[key] == temp]
	return max(l)


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		d[i] = d.get(i, 0) + 1
	v = d.values()
	keys = d.keys()
	lfre = min(v)
	l = []
	for i in d:
		if d.get(i) == lfre:
			l.append(i)
	return max(l)


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


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	Min = min(d.values())
	ans = -99999999
	for (i, j) in d.items():
		if j == Min:
			ans = max(ans, i)
	return ans


def LargButMinFreq(arr: list, n: int) -> int:
	counter = {}
	for i in range(n):
		if arr[i] in counter:
			counter[arr[i]] += 1
		else:
			counter[arr[i]] = 1
	min_frequency = float('inf')
	for value in counter.values():
		if value < min_frequency:
			min_frequency = value
	max_number = 0
	for (key, value) in counter.items():
		if value == min_frequency and key > max_number:
			max_number = key
	return max_number


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	(Max, MinFreq) = (0, min(d.values()))
	for i in d:
		if d[i] == MinFreq and i > Max:
			Max = i
	return Max


import collections

def LargButMinFreq(arr, n):
	Map = collections.Counter(arr)
	Min = min(list(Map.values()))
	rand = []
	for (key, value) in Map.items():
		if value == Min:
			rand.append(key)
	return max(rand)


def LargButMinFreq(arr, n):
	dic = {}
	l = []
	for i in arr:
		if i not in dic:
			dic[i] = 1
		else:
			dic[i] += 1
	m = min(dic.values())
	for (k, v) in dic.items():
		if v == m:
			l.append(k)
	return max(l)


import math

def LargButMinFreq(arr, n):
	numResult = -1
	numFreq = math.inf
	counts = {}
	for item in arr:
		if item not in counts:
			counts[item] = 1
		else:
			counts[item] += 1
	for num in counts:
		if counts[num] < numFreq:
			numFreq = counts[num]
			numResult = num
		elif counts[num] == numFreq:
			if num > numResult:
				numResult = num
	return numResult


import collections

def LargButMinFreq(arr, n):
	frequencies = collections.Counter(arr)
	return min(frequencies.keys(), key=lambda item: (frequencies[item], -item))


def LargButMinFreq(arr, n):
	from collections import Counter
	b = Counter(arr)
	c = list(b.keys())
	d = list(b.values())
	x = min(d)
	y = []
	for i in range(len(d)):
		if d[i] == x:
			p = i
			g = c[p]
			y.append(g)
	return max(y)


def LargButMinFreq(a, n):
	b = {}
	for i in a:
		if i not in b:
			b[i] = 1
		else:
			b[i] += 1
	mn = b[a[0]]
	k = a[0]
	for i in b:
		if mn > b[i]:
			mn = b[i]
			k = i
		elif mn == b[i] and k < i:
			k = i
			mn = b[i]
	return k


from collections import Counter

def LargButMinFreq(arr, n):
	hashMap = Counter(arr)
	temp = [[val, -key] for (key, val) in hashMap.items()]
	temp = sorted(temp, key=lambda x: (x[0], x[1]))
	return -temp[0][1]


def LargButMinFreq(arr, n):
	dictA = {}
	listA = []
	for i in range(n):
		if arr[i] in dictA.keys():
			dictA[arr[i]] += 1
		else:
			dictA[arr[i]] = 1
	var = min(dictA.values())
	for (i, j) in dictA.items():
		if j == var:
			listA.append(i)
	return max(listA)


def LargButMinFreq(arr, n):
	dir = {}
	for i in arr:
		if i in dir:
			dir[i] = dir[i] + 1
		else:
			dir[i] = 1
	mina = min(dir.values())
	max = 0
	for (i, j) in dir.items():
		if j == mina:
			if i > max:
				max = i
	return max


def LargButMinFreq(arr, n):
	table = {}
	for i in arr:
		table[i] = table.get(i, 0) + 1
	max_num = 0
	min_freq = n
	for (num, freq) in table.items():
		if freq < min_freq:
			max_num = num
			min_freq = freq
		if freq == min_freq:
			max_num = max(max_num, num)
	return max_num


def LargButMinFreq(arr, n):
	d = {}
	for ele in arr:
		if ele in d:
			d[ele] += 1
		else:
			d[ele] = 1
	t = min(d.values())
	ans = 0
	for (ele, value) in d.items():
		if value == t and ele > ans:
			ans = ele
	return ans


def LargButMinFreq(arr, n):
	hm = dict()
	for v in arr:
		if v in hm:
			hm[v] += 1
		else:
			hm[v] = 1
	mf = 9223372036854775807
	for v in hm:
		if hm[v] < mf:
			mf = hm[v]
	mv = -9223372036854775807
	for v in hm:
		if hm[v] == mf and v > mv:
			mv = v
	return mv


def LargButMinFreq(arr, n):
	maxv = max(arr)
	hash_freq = [0] * (maxv + 1)
	for v in arr:
		if hash_freq[v] != 0:
			hash_freq[v] += 1
		else:
			hash_freq[v] = 1
	min_freq = 1000
	for e in hash_freq:
		if (e != 0) & (min_freq > e):
			min_freq = e
	arr.sort(reverse=True)
	for e in arr:
		if hash_freq[e] == min_freq:
			return e


def LargButMinFreq(arr, n):
	s = {}
	for i in arr:
		if i in s:
			s[i] += 1
		else:
			s[i] = 1
	p = 999999999
	for i in s:
		if s[i] < p:
			p = s[i]
	m = -1
	g = -999999
	for i in s:
		if s[i] == p and i > g:
			g = i
	return g


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		try:
			d[i] += 1
		except:
			d[i] = 1
	fm = float('inf')
	m = -float('inf')
	for i in d:
		if d[i] < fm:
			m = i
			fm = d[i]
		elif d[i] == fm:
			if i > m:
				m = i
	return m


from collections import defaultdict

def LargButMinFreq(arr, n):
	di = defaultdict(int)
	for i in arr:
		di[i] += 1
	mini = n + 1
	minimum = -1
	for i in di:
		if di[i] == mini:
			minimum = max(i, minimum)
		if di[i] < mini:
			mini = di[i]
			minimum = i
	return minimum


def LargButMinFreq(arr, n):
	arr.sort()
	tem = 0
	ct = 1100000
	re = arr[0]
	if len(arr) == 1:
		return re
	for i in range(n):
		if i == n - 1:
			if arr[i] > re and arr[i] != arr[i - 1]:
				re = arr[i]
			elif arr[i] == arr[i - 1]:
				tem += 1
				if tem <= ct:
					re = arr[i]
		elif arr[i] == arr[i + 1]:
			tem += 1
		else:
			if tem <= ct:
				re = arr[i]
				ct = tem
			tem = 0
	return re


def LargButMinFreq(arr, n):
	h = {}
	for i in arr:
		if i not in h:
			h[i] = 1
		else:
			h[i] += 1
	h = sorted(h.items(), key=lambda x: x[1])
	ans = -1
	frequency = 99999
	for i in h:
		x = list(i)
		if x[0] > ans and x[1] <= frequency:
			ans = x[0]
			frequency = x[1]
	return ans


from collections import Counter

def LargButMinFreq(arr, n):
	freq = Counter(arr)
	minf = float('inf')
	mina = -1
	for i in freq:
		if freq[i] < minf:
			minf = freq[i]
			mina = i
		elif freq[i] == minf and i > mina:
			mina = i
	return mina
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(LargButMinFreq(arr, n))


def LargButMinFreq(arr, n):
	map = {}
	ans = 99999
	result = []
	for i in arr:
		if i in map.keys():
			map[i] += 1
		else:
			map[i] = 1
	for (key, value) in map.items():
		ans = min(value, ans)
	for (key, value) in map.items():
		if map[key] == ans:
			result.append(key)
	return max(result)
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(LargButMinFreq(arr, n))


def LargButMinFreq(arr, n):
	d = {}
	for i in range(0, n):
		if arr[i] not in d:
			d[arr[i]] = 1
		else:
			d[arr[i]] += 1
	freq = min(d.values())
	m = 0
	for i in range(0, n):
		if d[arr[i]] == freq:
			if arr[i] > m:
				m = arr[i]
	return m
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(LargButMinFreq(arr, n))


def LargButMinFreq(arr, n):
	mp = dict()
	for i in arr:
		if i in mp:
			mp[i] += 1
		else:
			mp[i] = 1
	ele = 0
	freq = 100000
	for i in mp:
		if mp[i] < freq:
			freq = mp[i]
			ele = i
		if mp[i] == freq:
			ele = max(ele, i)
	return ele
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(LargButMinFreq(arr, n))


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	count = 999999
	for i in d:
		if d[i] < count:
			count = d[i]
	ans = -1
	for i in d:
		if d[i] == count and i > ans:
			ans = i
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(LargButMinFreq(arr, n))


def LargButMinFreq(arr, n):
	map = {}
	for i in arr:
		if i in map:
			map[i] += 1
		else:
			map[i] = 1
	ans = [float('-inf'), float('inf')]
	for (values, freq) in map.items():
		if freq < ans[1]:
			ans[0] = values
			ans[1] = freq
		elif freq == ans[1]:
			if values > ans[0]:
				ans[0] = values
				ans[1] = freq
	return ans[0]


def LargButMinFreq(arr, n):
	d = {}
	for x in arr:
		if not x in d:
			d[x] = 1
		else:
			d[x] += 1
	return sorted(d.items(), key=lambda x: (x[1], -x[0]))[0][0]


def LargButMinFreq(arr, n):
	m = {}
	minm = 999999
	for i in arr:
		if i in m:
			m[i] += 1
		else:
			m[i] = 1
	for (i, v) in m.items():
		if v < minm:
			minm = v
	ans = -1 << 32
	for (i, v) in m.items():
		if v == minm and ans < i:
			ans = i
	return ans


def LargButMinFreq(arr, n):
	cnts = {}
	for v in arr:
		cnts[v] = cnts.get(v, 0) + 1
	return sorted(cnts.items(), key=lambda x: [x[1], -x[0]])[0][0]


from collections import defaultdict

def LargButMinFreq(arr, n):
	d = defaultdict(int)
	for i in arr:
		d[i] += 1
	max1 = -1000000
	minfrq = 1000000
	ans = []
	for i in d:
		if d[i] <= minfrq:
			minfrq = d[i]
	for i in d:
		if d[i] == minfrq:
			ans.append(i)
	return max(ans)


from collections import defaultdict

def LargButMinFreq(arr, n):
	hmap = defaultdict(int)
	for i in range(n):
		hmap[arr[i]] += 1
	min_freq = float('inf')
	max_val = float('-inf')
	for elem in hmap:
		num = elem
		freq = hmap[elem]
		if freq < min_freq:
			min_freq = freq
			max_val = num
		elif freq == min_freq and max_val < num:
			max_val = num
	return max_val


def LargButMinFreq(arr, n):
	m = {}
	for i in arr:
		if i in m:
			m[i] = m[i] + 1
		else:
			m[i] = 1
	a = m.values()
	b = min(a)
	ma = min(arr)
	for i in m:
		if m[i] == b:
			ma = max(ma, i)
	return ma


def LargButMinFreq(arr, n):
	hashmap = {}
	for i in arr:
		if i in hashmap:
			hashmap[i] += 1
		else:
			hashmap[i] = 1
	min_freq = min(list(hashmap.values()))
	min_val = 0
	for i in list(hashmap.keys()):
		if hashmap[i] == min_freq:
			if i > min_val:
				min_val = i
	return min_val


from collections import Counter

def LargButMinFreq(arr, n):
	ans = []
	di = Counter(arr)
	chota = min(di.values())
	for (key, value) in di.items():
		if value == chota:
			ans.append(key)
	return max(ans)


from collections import Counter

def LargButMinFreq(arr, n):
	d = Counter(arr)
	(val, f) = (-1, n + 1)
	for i in d.keys():
		if d[i] < f:
			f = d[i]
			val = i
		elif d[i] == f:
			val = max(val, i)
	return val


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if d.get(i):
			d[i] += 1
		else:
			d[i] = 1
	(val, f) = (-1, n + 1)
	for i in d.keys():
		if d[i] < f:
			f = d[i]
			val = i
		elif d[i] == f:
			val = max(val, i)
	return val


def LargButMinFreq(arr, n):
	m = dict()
	for a in arr:
		if a in m:
			m[a] += 1
		else:
			m[a] = 1
	min_freq = 1000000
	max_val = 0
	for (val, freq) in m.items():
		if freq < min_freq:
			min_freq = freq
			max_val = val
		elif freq == min_freq:
			if val > max_val:
				max_val = val
	return max_val


from collections import Counter

def LargButMinFreq(arr, n):
	counts = Counter(arr)
	min_val = min(counts.values())
	only_min_keys = [key for (key, val) in counts.items() if val == min_val]
	return max(only_min_keys)


def LargButMinFreq(arr, n):
	from collections import Counter
	d = Counter(arr)
	mi = min(d.values())
	ans = 0
	for val in d.keys():
		if d[val] == mi:
			ans = max(ans, val)
	return ans


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		if i in d.items():
			d[i] += 1
		else:
			d[i] = 1
	max_val = max([k for k in d.keys() if d[k] == 1])
	return max_val


def LargButMinFreq(arr, n):
	l = []
	for i in arr:
		if arr.count(i) == 1:
			l.append(i)
	return max(l)


def LargButMinFreq(arr, n):
	d = {}
	for i in arr:
		try:
			d[i] += 1
		except:
			d[i] = 1
	mi = min(list(d.values()))
	arr = []
	for i in list(d.keys()):
		if d[i] == mi:
			arr.append(i)
	return max(arr)


def LargButMinFreq(arr, n):
	h = {}
	ans = []
	for i in arr:
		if i not in h:
			h[i] = 1
		else:
			h[i] += 1
	for (k, v) in h.items():
		if v == 1:
			ans.append(k)
	return max(ans)


def LargButMinFreq(arr, n):
	arr1 = []
	arr2 = []
	for i in arr:
		if i not in arr1:
			arr2.append(arr.count(i))
	minvalue = min(arr2)
	for i in arr:
		if arr.count(i) == minvalue:
			arr1.append(i)
	return max(arr1)


from collections import defaultdict

def LargButMinFreq(arr, n):
	d = defaultdict(int)
	for i in arr:
		d[i] += 1
	mn = 10000000
	index = -1
	for (i, j) in d.items():
		if mn > j:
			index = i
			mn = j
		elif mn == j and index < i:
			index = i
	return index


import math

def LargButMinFreq(arr, n):
	freq = {}
	(minFreq, maxElem) = (math.inf, -math.inf)
	for a in arr:
		if a not in freq:
			freq[a] = 1
		else:
			freq[a] += 1
		if freq[a] < minFreq:
			minFreq = freq[a]
	for e in freq:
		if freq[e] == minFreq and e > maxElem:
			maxElem = e
	return maxElem


def LargButMinFreq(arr, n):
	d = {}
	l = []
	freq = 1
	for i in arr:
		if i in d:
			d[i] += 1
			freq = min(freq, d[i])
		else:
			d[i] = 1
			freq = 1
	for i in d:
		if d[i] == freq:
			l.append(i)
	return max(l)


def LargButMinFreq(arr, n):
	return max(list(set(arr)))