Given an array where every element appears twice except a pair (two elements). return the elements of this unique pair in sorted order.
 
Example 1:
Input : Arr[] = {2, 2, 5, 5, 6, 7}
Output : 6 7
Explanation:
We can see we have [2, 2, 5, 5, 6, 7]. 
Here 2 and 5 are coming two times. 
So, the answer will be 6 7.
Example 2:
Input : Arr[] = {1, 3, 4, 1}
Output : 3 4
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function findUniquePair() that takes an array (arr), sizeOfArray (n) and return the unique pair's element in increasing order. The driver code takes care of the printing.
 
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{7}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

class Solution:
    def findUniquePair(self, arr, n): 
      
        # Complete the function

Solutions:
from collections import Counter

class Solution:

	def findUniquePair(self, arr, n):
		result = [item for (items, c) in Counter(arr).most_common() for item in [items] * c]
		a = []
		a.append(result[n - 2])
		a.append(result[n - 1])
		a.sort()
		return a


from collections import Counter

class Solution:

	def findUniquePair(self, arr, n):
		c = Counter()
		for a in arr:
			c[a] += 1
		ans = []
		for (k, v) in c.most_common():
			if v == 1:
				ans.append(k)
		if ans[0] > ans[1]:
			(ans[0], ans[1]) = (ans[1], ans[0])
		return ans


from collections import Counter

class Solution:

	def findUniquePair(self, arr, n):
		c = Counter(arr)
		output = []
		for (item, count) in c.items():
			if count % 2:
				output.append(item)
		return sorted(output)


class Solution:

	def findUniquePair(self, arr, n):
		dict = {}
		result = []
		for i in range(n):
			dict[arr[i]] = dict.get(arr[i], 0) + 1
		for i in range(n):
			if dict[arr[i]] == 1:
				result.append(arr[i])
		result.sort()
		return result


class Solution:

	def findUniquePair(self, arr, n):
		from collections import Counter
		f = Counter(arr)
		ans = []
		for i in f:
			if f[i] == 1:
				ans.append(i)
		ans.sort()
		return ans


class Solution:

	def findUniquePair(self, arr, n):
		dic = {}
		for i in arr:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		lis = []
		for i in dic:
			if dic[i] < 2:
				lis.append(i)
		lis.sort()
		return lis


class Solution:

	def findUniquePair(self, arr, n):
		res = {}
		a = []
		for ele in arr:
			if ele not in res:
				res[ele] = 0
			res[ele] += 1
		for (i, j) in res.items():
			if j == 1:
				a.append(i)
		if len(a) == 1:
			a.insert(0, 0)
		elif len(a) == 2:
			if a[0] > a[1]:
				(a[0], a[1]) = (a[1], a[0])
		return a


from collections import Counter

class Solution:

	def findUniquePair(self, arr, n):
		d = Counter(arr)
		a = []
		for i in arr:
			if d[i] == 1:
				a.append(i)
		a.sort()
		return a


class Solution:

	def findUniquePair(self, arr, n):
		d = {}
		res = []
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in range(n):
			if d[arr[i]] == 1:
				res.append(arr[i])
		res.sort()
		return res


class Solution:

	def findUniquePair(self, arr, n):
		XOR = arr[0]
		for i in range(1, n):
			XOR = XOR ^ arr[i]
		set_bit_no = XOR & ~(XOR - 1)
		x = 0
		y = 0
		for i in range(n):
			if arr[i] & set_bit_no:
				x ^= arr[i]
			else:
				y ^= arr[i]
		ans = []
		if x < y:
			ans.append(x)
			ans.append(y)
		else:
			ans.append(y)
			ans.append(x)
		return ans


from collections import Counter

class Solution:

	def findUniquePair(self, arr, n):
		m = Counter(arr)
		l = []
		for i in arr:
			if m[i] == 1:
				l.append(i)
		l.sort()
		return l


class Solution:

	def findUniquePair(self, arr, n):
		d = dict()
		a = []
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] = 2
		for i in d:
			if d[i] == 1:
				a.append(i)
		return sorted(a)


class Solution:

	def findUniquePair(self, arr, n):
		res = 0
		for i in range(n):
			res = res ^ arr[i]
		c = 1
		while res:
			if res & 1:
				x = c
				break
			c += 1
			res = res >> 1
		r1 = 0
		r2 = 0
		for i in arr:
			if i & 1 << x - 1:
				r1 ^= i
			else:
				r2 ^= i
		a = min(r1, r2)
		b = max(r1, r2)
		return (a, b)