You are given an array A of size N. In one operation, you can do the following:
Select indices i and j (i\neq j) and set A_{i} = A_{j}.  

Find the minimum number of operations required to make all elements of the array equal.

------ Input Format ------ 

- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains an integer N — the size of the array.
- The next line contains N space-separated integers, denoting the array A.

------ Output Format ------ 

For each test case, output on a new line, the minimum number of operations required to make all elements of the array equal.

------ Constraints ------ 

$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 2\cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.

----- Sample Input 1 ------ 
3
3
1 2 3
4
2 2 3 1
4
3 1 2 4

----- Sample Output 1 ------ 
2
2
3

----- explanation 1 ------ 
Test case $1$: The minimum number of operations required to make all elements of the array equal is $2$. A possible sequence of operations is:
- Select indices $1$ and $2$ and set $A_{1} = A_{2} = 2$.
- Select indices $3$ and $2$ and set $A_{3} = A_{2} = 2$.

Thus, the final array is $[2, 2, 2]$.

Test case $2$: The minimum number of operations required to make all elements of the array equal is $2$. A possible sequence of operations is:
- Select indices $3$ and $2$ and set $A_{3} = A_{2} = 2$.
- Select indices $4$ and $3$ and set $A_{4} = A_{3} = 2$.

Thus, the final array is $[2, 2, 2, 2]$.

Test case $3$: The minimum number of operations required to make all elements of the array equal is $3$. A possible sequence of operations is:
- Select indices $2$ and $1$ and set $A_{2} = A_{1} = 3$.
- Select indices $3$ and $1$ and set $A_{3} = A_{1} = 3$.
- Select indices $4$ and $1$ and set $A_{4} = A_{1} = 3$.

Thus, the final array is $[3, 3, 3, 3]$.

Source: codechef | Difficulty: EASY
Tags: Data structures


Solutions:
from collections import Counter
T = int(input())
for _ in range(T):
	N = int(input())
	answer = 0
	A = list(map(int, input().split(' ')))
	counts = Counter(A).most_common()
	freq = counts[0][1]
	print(N - freq)


from collections import Counter
T = int(input())
for _ in range(T):
	input()
	answer = 0
	A = list(map(int, input().split(' ')))
	X = Counter(A).most_common()
	Z = X[0][0]
	for i in A:
		if i != Z:
			answer += 1
	print(answer)


from collections import Counter
t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	b = Counter(a)
	ans = max(b.values())
	print(n - ans)


from collections import Counter
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = Counter(arr)
	maxi = max(c.values())
	print(n - maxi)


n = int(input())
for i in range(n):
	length = input()
	inp_list = input().split()
	dicti = {}
	for num in inp_list:
		dicti.setdefault(num, 0)
		dicti[num] += 1
	print(len(inp_list) - max(dicti.values()))


for i in range(int(input())):
	n = int(input())
	l = list(input().split())
	d = dict()
	for i in l:
		if i in d.keys():
			d[i] += 1
		else:
			d[i] = 1
	print(n - max(d.values()))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	maxval = max(d.values())
	print(n - maxval)


for jdfg in range(int(input())):
	n = int(input())
	k = list(map(int, input().split()))
	d = {}
	for j in k:
		if j in d:
			d[j] += 1
		else:
			d[j] = 1
	l = list(d.values())
	print(n - max(l))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	ma = max(c.values())
	print(n - ma)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	d = defaultdict(int)
	for i in [int(x) for x in input().split()]:
		d[i] += 1
	print(n - max(d.values()))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	m = max(d.values())
	print(len(l) - m)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = {}
	for j in a:
		if j in c:
			c[j] += 1
		else:
			c[j] = 1
	s = max(c.values())
	print(n - s)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = max(Counter(l).values())
	print(n - d)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	if n == len(set(a)):
		print(n - 1)
	else:
		ca = Counter(a)
		m = max(ca.values())
		result = n - m
		print(result)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	t = len(set(a))
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 1
		else:
			d[a[i]] += 1
	t1 = max(d.values())
	if n == t:
		print(n - 1)
	else:
		print(n - t1)


t = int(input())
for i in range(t):
	n = int(input())
	a = map(int, input().split())
	dict = {}
	for j in a:
		if j in dict:
			dict[j] += 1
		else:
			dict[j] = 1
	value = max(dict.values())
	print(n - value)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mydict = {}
	for item in a:
		if item in mydict:
			mydict[item] += 1
		else:
			mydict[item] = 1
	value = max(mydict.values())
	print(n - value)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	y = max(c.values())
	print(len(a) - y)


T = int(input())
for i in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	D = {}
	for j in A:
		if j in D:
			D[j] += 1
		else:
			D[j] = 1
	m = max(D.values())
	print(n - m)


for t in range(int(input())):
	d = {}
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(len(a)):
		if a[i] in d.keys():
			d[a[i]] = d[a[i]] + 1
		else:
			d[a[i]] = 1
	v = list(d.values())
	m = 0
	f = -1
	for i in range(len(v)):
		if v[i] > m:
			f = i
			m = v[i]
	print(n - m)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	C = {}
	for i in A:
		if i not in C:
			C[i] = 1
		else:
			C[i] += 1
	maxC = max(C.values())
	print(N - maxC)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	C = Counter(arr)
	maximum = max(C.values())
	print(n - maximum)


from collections import Counter
for i in range(int(input())):
	l = int(input())
	s = list(map(int, input().split()))
	c = Counter(s)
	maxc = max(c.values())
	print(l - maxc)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))[:n]
	c = 0
	d = {}
	for k in l:
		if k not in d:
			d[k] = 1
		else:
			d[k] += 1
	a = max(d.values())
	print(n - a)


for i in range(int(input())):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	(f, mf) = (1, 1)
	for i in range(1, n):
		if l[i] == l[i - 1]:
			f += 1
		else:
			if mf < f:
				mf = f
			f = 1
	print(n - max(mf, f))


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	c = Counter(a)
	max_count = max(c.values())
	print(n - max_count)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	c = Counter(a)
	max_count = c.most_common()[0][1]
	print(n - max_count)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	print(n - max(d.values()))


for i in range(int(input())):
	t = int(input())
	l = list(map(int, input().split()))
	l1 = list(set(l))
	c = []
	if len(l) == len(l1):
		print(len(l) - 1)
	else:
		for i in l1:
			c.append(l.count(i))
		print(t - max(c))


from collections import Counter
T = int(input())
for i in range(T):
	N = int(input())
	a = list(map(int, input().split(' ')))[:N]
	a = dict(Counter(a))
	print(N - max(a.values()))


t = int(input())
for _ in range(t):
	sz = int(input())
	N = [int(x) for x in input().split(' ')]
	d = dict({})
	for i in N:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	print(len(N) - max(list(d.values())))


T = int(input())
for i in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	f = {}
	for i in range(n):
		if a[i] not in f:
			f[a[i]] = 0
		f[a[i]] += 1
	m = max(f.values())
	print(n - m)


t = int(input())
while t > 0:
	N = int(input())
	A = list(map(int, input().split()))
	f = {}
	for i in range(N):
		if A[i] not in f:
			f[A[i]] = 0
		f[A[i]] += 1
	m = max(f.values())
	print(N - m)
	t = t - 1


def min_operations(arr):
	n = len(arr)
	count = {}
	for i in arr:
		if i in count:
			count[i] += 1
		else:
			count[i] = 1
	max_count = max(count.values())
	return n - max_count
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(min_operations(arr))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	count = Counter(lst)
	maximumamongcount = max(count.values())
	print(len(lst) - maximumamongcount)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	x = Counter(arr)
	max_value = max(x.values())
	print(n - max_value)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(Counter(a).values())
	print(len(a) - m)


from collections import Counter
testcases = int(input())
while testcases > 0:
	N = int(input())
	li = list(map(int, input().split()))[:N]
	maximum = max(Counter(li).values())
	print(len(li) - maximum)
	testcases -= 1


for i in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = [0] * (max(l) + 1)
	for _ in l:
		f[_] = f[_] + 1
	print(sum(f) - max(f))


def maxx(l):
	maxi = 0
	for i in range(len(l)):
		c = l.count(l[i])
		if c > maxi:
			maxi = c
	return maxi
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = Counter(l)
	ans = max(a.values())
	print(n - ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	max1 = max(Counter(l).values())
	print(n - max1)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	d = dict()
	for ele in arr:
		if ele in d:
			d[ele] += 1
		else:
			d[ele] = 1
	maxfreq = 0
	for (k, v) in d.items():
		if v > maxfreq:
			maxfreq = v
	print(len(arr) - maxfreq)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	freq = Counter(a)
	maximum = max(freq.values())
	print(len(a) - maximum)


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	k = max(d.values())
	print(n - k)


from collections import Counter
T = int(input())
for i in range(T):
	n = int(input())
	lst = list(map(int, input().split()))
	arr = Counter(lst)
	print(n - max(arr.values()))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = Counter(l)
	x = max([c[i] for i in c])
	print(n - x)


from collections import *
for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	f = Counter(b)
	p = max(f.values())
	print(a - p)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = Counter(arr)
	mux = max(dic.values())
	print(n - mux)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	maxC = {}
	for i in A:
		if i in maxC:
			maxC[i] += 1
		else:
			maxC[i] = 1
	maxV = max(maxC.values())
	print(len(A) - maxV)


test = int(input())
for i in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * (n + 1)
	for i in range(n):
		num = a[i]
		b[num] += 1
	maxi = max(b)
	print(n - maxi)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = {}
	for i in a:
		if i not in c:
			c[i] = 1
		else:
			c[i] += 1
	maxC = max(c.values())
	print(n - maxC)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	C = Counter(a)
	maxi = max(C.values())
	print(n - maxi)


test = int(input())
for i in range(test):
	number = int(input())
	a = list(map(int, input().split()))
	freq_dict = {}
	for i in a:
		freq_dict[i] = freq_dict.get(i, 0) + 1
	repetited_value = max(freq_dict, key=freq_dict.get)
	count = 0
	for x in a:
		if x != repetited_value:
			count += 1
	print(count)


for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	k = max(d.values())
	print(n - k)


n = int(input())
l = []
for i in range(n):
	m = int(input())
	l = list(map(int, input().split()))
	count = {}
	for i in l:
		if i not in count:
			count[i] = 1
		else:
			count[i] += 1
	result = max(count.values())
	print(m - result)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	freq = Counter(l)
	m = max(freq.values())
	print(len(l) - m)


from collections import Counter
for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	C = Counter(A)
	maxC = max(C.values())
	print(N - maxC)


goals = int(input())
for distractions in range(goals):
	wife = int(input())
	life = list(map(int, input().split()))
	aim = {}
	for death in life:
		if death not in aim:
			aim[death] = 1
		else:
			aim[death] += 1
	stress = max(aim.values())
	print(len(life) - stress)


t = int(input())
for i in range(t):
	x = int(input())
	list1 = list(list(map(int, input().split())))
	cnt = 0
	dict1 = {}
	for i in list1:
		if i in dict1:
			dict1[i] += 1
		else:
			dict1[i] = 1
	maxi = 0
	for k in dict1.keys():
		if dict1[k] > maxi:
			maxi = dict1[k]
	print(x - maxi)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if li[i] in d:
			d[li[i]] += 1
		else:
			d[li[i]] = 1
	m = max(d.values())
	print(n - m)


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list(map(int, input().split()))
	freq = {}
	for a in list1:
		if a in freq:
			freq[a] += 1
		else:
			freq[a] = 1
	print(n - max(freq.values()))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	r = Counter(a)
	maximum = max(r.values())
	print(n - maximum)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	freq = {}
	for num in a:
		freq[num] = freq.get(num, 0) + 1
	minOP = float('inf')
	for (_, f) in freq.items():
		minOP = min(minOP, n - f)
	print(minOP)


from collections import defaultdict

def min_operations(n, arr):
	freq = defaultdict(int)
	for num in arr:
		freq[num] += 1
	max_freq = max(freq.values())
	return n - max_freq
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(min_operations(n, arr))


import sys
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7

def readInt():
	return int(input())

def readIntList():
	return list(map(int, input().split()))

def readStringList():
	return list(input())

def readStringListWithSpace():
	return list(input().split())

def readString():
	return input()
import math

def execute():
	for _ in range(readInt()):
		n = readInt()
		a = readIntList()
		hmap = {}
		for c in a:
			if c in hmap:
				hmap[c] += 1
			else:
				hmap[c] = 1
		max_same = 0
		for (k, v) in hmap.items():
			if v > max_same:
				max_same = v
		print(n - max_same)
execute()