Given an array of size n which contains all elements occurring in multiples of K, except one element which doesn't occur in multiple of K. Find that unique element.
 
Example 1:
Input : 
n = 7, k = 3
arr[] = {6, 2, 5, 2, 2, 6, 6}
Output : 
5
Explanation:
Every element appears 3 times except 5.
 
Example 2:
Input  : 
n = 5, k = 4
arr[] = {2, 2, 2, 10, 2}
Output :
10
Explanation:
Every element appears 4 times except 10.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function findUnique() which takes the array A[], its size N and an integer K as inputs and returns the element with single occurence.
 
Expected Time Complexity: O(N. Log(A[i]) )
Expected Auxiliary Space: O( Log(A[i]) )
 
Constraints:
3<= N<=2*10^{5}
2<= K<=2*10^{5}
1<= A[i]<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: class Solution:
    def findUnique(self, a, n, k): 
        #Code here

Solutions:
class Solution:

	def findUnique(self, a, n, k):
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in d:
			if d[i] % k != 0:
				return i


class Solution:

	def findUnique(self, a, n, k):
		b = {}
		for i in a:
			if i in b:
				b[i] += 1
			else:
				b[i] = 1
		for (i, j) in b.items():
			if j % k != 0:
				return i


class Solution:

	def findUnique(self, a, n, k):
		d1 = {}
		for i in a:
			if i not in d1:
				d1[i] = 1
			else:
				d1[i] += 1
		for i in d1.values():
			if i % k != 0:
				return list(d1.keys())[list(d1.values()).index(i)]


import collections

class Solution:

	def findUnique(self, a, n, k):
		dict1 = collections.Counter(a)
		for i in dict1:
			if dict1[i] % k != 0:
				return i


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):
		t = Counter(a)
		for (i, j) in t.items():
			if j % k != 0:
				return i


class Solution:

	def findUnique(self, a, n, k):
		d = {}
		for ele in a:
			if ele in d:
				d[ele] += 1
			else:
				d[ele] = 1
		for key in d:
			if d[key] % k != 0:
				return key


class Solution:

	def findUnique(self, a, n, k):
		d = {}
		arr = []
		for i in a:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for j in d:
			if d[j] % k != 0:
				arr.append(j)
		arr.sort()
		return arr[0]


class Solution:

	def findUnique(self, a, n, k):
		count = {}
		for v in a:
			if v in count:
				count[v] += 1
			else:
				count[v] = 1
		for key in count:
			if count[key] % k != 0:
				return key
		return -1


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):
		b = Counter(a)
		for (key, value) in b.items():
			if value % k != 0:
				return key


class Solution:

	def findUnique(self, a, n, k):
		l = list(set(a))
		l.sort()
		d = {}
		for i in l:
			d[i] = 0
		for i in a:
			d[i] = d[i] + 1
		for j in d:
			if d[j] % k != 0:
				return j


class Solution:

	def findUnique(self, a, n, k):
		result = -1
		test = {}
		for i in range(0, n):
			if a[i] in test.keys():
				test[a[i]] += 1
			if a[i] not in test.keys():
				test[a[i]] = 1
		for i in range(0, n):
			if test[a[i]] % k != 0:
				return a[i]


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):
		ac = Counter(a)
		for (v, f) in ac.items():
			if f % k:
				return v
		return -1


class Solution:

	def findUnique(self, a, n, k):
		hm = {}
		for i in range(n):
			try:
				hm[a[i]] += 1
			except:
				hm[a[i]] = 1
		for i in hm:
			if hm[i] % k != 0:
				return i


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):
		d = Counter(a)
		for (e, v) in d.items():
			if v % k != 0:
				return e


class Solution:

	def findUnique(self, a, n, k):
		dic = {}
		for i in a:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		for (m, n) in dic.items():
			if n % k != 0:
				return m


class Solution:

	def findUnique(self, a, n, k):
		s = {}
		for i in a:
			if i not in s:
				s[i] = 1
			else:
				s[i] += 1
		for j in s:
			if s[j] % k != 0:
				return j


class Solution:

	def findUnique(self, a, n, k):
		dic = {}
		for ele in a:
			dic[ele] = dic.get(ele, 0) + 1
		for ele in dic:
			if dic[ele] % k != 0:
				return ele


class Solution:

	def findUnique(self, a, n, k):
		dict_ = {}
		for elem in a:
			dict_.update({elem: (dict_.get(elem, 0) + 1) % k})
			if dict_[elem] == 0:
				del dict_[elem]
		return list(dict_.keys())[0]


class Solution:

	def findUnique(self, a, n, k):
		dict_ = {}
		for elem in a:
			dict_.update({elem: dict_.get(elem, 0) + 1})
		list_ = list(dict_.values())
		val = list(filter(lambda x: x % k != 0, list_))[0]
		ind = list_.index(val)
		return list(dict_.keys())[ind]


class Solution:

	def findUnique(self, a, n, k):
		d = dict()
		for i in a:
			d[i] = 0
		for i in a:
			d[i] += 1
		for i in d.keys():
			if d[i] % k != 0:
				return i


class Solution:

	def findUnique(self, a, n, k):
		a.sort()
		for i in a[::k]:
			if a.count(i) % k != 0:
				return i
		return 0


class Solution:

	def findUnique(self, a, n, k):
		l = {}
		for i in range(n):
			if a[i] in l:
				l[a[i]] += 1
			else:
				l[a[i]] = 1
		for i in l:
			if l[i] % k != 0:
				return i


class Solution:

	def findUnique(self, a, n, k):
		unique = {}
		for i in range(n):
			if a[i] not in unique:
				unique[a[i]] = 1
			else:
				unique[a[i]] += 1
		for key in unique:
			if unique[key] % k != 0:
				return key
		return -1


class Solution:

	def findUnique(self, a, n, k):
		freq = {}
		for i in a:
			try:
				freq[i] = freq.get(i, 0) + 1
			except:
				freq[i] = 1
		for (key, val) in freq.items():
			if freq[key] % k != 0:
				return key


class Solution:

	def findUnique(self, a, n, k):
		dict1 = {}
		temp1 = -1
		for i in a:
			if i not in dict1:
				dict1[i] = 1
			else:
				dict1[i] += 1
		for j in dict1.keys():
			if dict1[j] % k != 0:
				temp1 = j
				break
		return temp1


class Solution:

	def findUnique(self, a, n, k):
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for (i, v) in d.items():
			if v != k and v % k == 1:
				return i


class Solution:

	def findUnique(self, a, n, k):
		freq = {}
		for i in range(len(a)):
			if a[i] not in freq.keys():
				freq[a[i]] = 1
			else:
				freq[a[i]] += 1
		for (item, values) in freq.items():
			if values % k != 0:
				return item
		return -1


def index(a):
	arr = [0] * 32
	for n in a:
		k = -1
		while n != 0:
			if n & 1 == 1:
				arr[k] += 1
			k -= 1
			n = n >> 1
	return arr

def calculate(a, k):
	a = index(a)
	for i in range(len(a) - 1, -1, -1):
		a[i] = a[i] % k
	return a

def printarr(a, k):
	ar = calculate(a, k)
	res = 0
	ar = ar[::-1]
	for i in range(len(ar)):
		res += ar[i] * 2 ** i
	return res

class Solution:

	def findUnique(self, a, n, k):
		res = printarr(a, k)
		return res


class Solution:

	def findUnique(self, a, n, k):
		l = {}
		for i in range(n):
			l[a[i]] = l.get(a[i], 0) + 1
		for (k1, v) in l.items():
			if v % k != 0:
				return k1
				break


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):
		return list({x: count for (x, count) in Counter(a).items() if count % k != 0})[0]


from collections import defaultdict

class Solution:

	def findUnique(self, a, n, k):
		dic = defaultdict()
		for i in a:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		for j in a:
			if dic[j] % k != 0:
				return j


class Solution:

	def count(self, a):
		s = {}
		for i in a:
			if i in s:
				s[i] += 1
			else:
				s[i] = 1
		return s

	def findUnique(self, a, n, k):
		ans = self.count(a)
		for i in ans:
			if ans[i] % k != 0:
				return i


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):
		(c, s) = (Counter(a), set(a))
		for i in s:
			if c[i] % k != 0:
				return i
		return -1


class Solution:

	def findUnique(self, a, n, k):
		d = {}
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = 1
			else:
				d[a[i]] += 1
		for (i, j) in d.items():
			if j % k != 0:
				return i


class Solution:

	def findUnique(self, a, n, k):
		freq = {}
		for i in a:
			if i in freq:
				freq[i] += 1
			else:
				freq[i] = 1
		for (key, values) in freq.items():
			if values % k != 0:
				return key


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):
		dict = Counter(a)
		for (key, count) in dict.items():
			if count % k != 0:
				return key


from collections import Counter

class Solution:

	def findUnique(self, a, n, k):

		def fact(num):
			if num % k == 0:
				return k
		dict = Counter(a)
		for key in dict.keys():
			if fact(dict[key]) != k:
				return key


class Solution:

	def findUnique(self, a, n, k):
		c = dict()
		for i in set(a):
			c[i] = 0
		for i in a:
			c[i] += 1
		for i in set(a):
			if int(c[i] % k) != 0:
				return i