You are given an integer array nums that may contain duplicates. Your task is to return all possible subsets. Return only unique subsets and they can be in any order.
Example: 
Input: 
nums = [1,2,2] 
Output: 
[[],[1],[1,2],[1,2,2],[2],[2,2]]
Explanation: 
We can have subsets ranging from length 0 to 3. which are listed above. Also the subset [1,2] appears twice but is printed only once as we require only unique subsets.
Your Task:
Complete the function vector> printUniqueSubset(), which takes  a vector nums and return a vector of vector consisting of all unique subsets.
Expected Time Complexity: O(K2^{N}).
Expected Auxiliary Space: O(K2^{N}).
Constraints:
1 <= nums.length <= 10
-10 <= nums[i] <= 10

Source: geeksforgeeks | Difficulty: MEDIUM

Starter code: #User function Template for python3



class Solution:

    def printUniqueSubset(self, nums):

        # Code here

Solutions:
class Solution:

	def printUniqueSubset(self, nums):
		n = len(nums)
		nums.sort()
		ans = []

		def solve(i, temp):
			ans.append(temp[:])
			for j in range(i, n):
				if j > i and nums[j] == nums[j - 1]:
					continue
				temp.append(nums[j])
				solve(j + 1, temp)
				temp.pop()
		solve(0, [])
		return ans


def subset2(arr, n, ans, ind, subs):
	if ind == n:
		return
	subs.append(arr[ind])
	ans.append(subs[:])
	subset2(arr, n, ans, ind + 1, subs)
	subs.pop()
	for i in range(ind + 1, n):
		if arr[i] == arr[i - 1]:
			continue
		subs.append(arr[i])
		ans.append(subs[:])
		subset2(arr, n, ans, i + 1, subs)
		subs.pop()

class Solution:

	def printUniqueSubset(self, nums):
		nums.sort()
		n = len(nums)
		ans = []
		subs = []
		ans.append(subs)
		i = 0
		subset2(nums, n, ans, i, subs)
		return ans


class Solution:

	def solve(self, k, nums, curr):
		if k == len(nums):
			self.ans.add(tuple(curr))
			return
		self.solve(k + 1, nums, curr)
		curr.append(nums[k])
		self.solve(k + 1, nums, curr)
		curr.pop()

	def printUniqueSubset(self, nums):
		nums.sort()
		self.ans = set()
		curr = []
		self.solve(0, nums, curr)
		return sorted(list(self.ans))


class Solution:

	def subsets(self, arr, index, seq):
		if index == len(arr):
			self.res.append(seq)
			return
		copyindex = index
		while index < len(arr) - 1 and arr[index] == arr[index + 1]:
			index += 1
		self.subsets(arr, index + 1, seq)
		self.subsets(arr, copyindex + 1, seq + [arr[copyindex]])

	def printUniqueSubset(self, nums):
		nums.sort()
		self.res = []
		self.subsets(nums, 0, [])
		return sorted(self.res)


class Solution:

	def printUniqueSubset(self, nums):

		def subdup(ind, nums, ans, s):
			ans.append(s[:])
			for i in range(ind, len(nums)):
				if i > ind and nums[i] == nums[i - 1]:
					continue
				s.append(nums[i])
				subdup(i + 1, nums, ans, s)
				s.pop()
			return
		ans = []
		s = []
		nums.sort()
		subdup(0, nums, ans, s)
		return ans


class Solution:

	def printUniqueSubset(self, nums):
		ans = []
		ds = []

		def findSubsets(ind: int):
			ans.append(ds[:])
			for i in range(ind, len(nums)):
				if i != ind and nums[i] == nums[i - 1]:
					continue
				ds.append(nums[i])
				findSubsets(i + 1)
				ds.pop()
		nums.sort()
		findSubsets(0)
		return ans


class Solution:

	def printUniqueSubset(self, nums):
		res = []
		nums.sort()

		def backtrack(i, subset):
			if i == len(nums):
				res.append(subset[:])
				return
			subset.append(nums[i])
			backtrack(i + 1, subset)
			subset.pop()
			while i + 1 < len(nums) and nums[i] == nums[i + 1]:
				i += 1
			backtrack(i + 1, subset)
		backtrack(0, [])
		res.sort()
		return res


class Solution:

	def gen(self, nums, n, arr, brr, pos):
		if pos == n:
			crr = brr.copy()
			arr.add(tuple(sorted(crr)))
			return
		brr.append(nums[pos])
		self.gen(nums, n, arr, brr, pos + 1)
		brr.pop()
		self.gen(nums, n, arr, brr, pos + 1)

	def printUniqueSubset(self, nums):
		arr = set()
		brr = []
		self.gen(nums, len(nums), arr, brr, 0)
		return sorted(list(arr))


class Solution:

	def printUniqueSubset(self, nums):
		(lst, tmp) = ([], [])
		dic = {}

		def subs(idx, nums, n, tmp, lst):
			if idx >= n:
				if str(tmp) not in dic:
					dic[str(tmp)] = 1
					lst.append(tmp[:])
				return
			tmp.append(nums[idx])
			subs(idx + 1, nums, n, tmp, lst)
			tmp.pop()
			subs(idx + 1, nums, n, tmp, lst)
		nums.sort()
		subs(0, nums, len(nums), tmp, lst)
		return sorted(lst)


class Solution:

	def printUniqueSubset(self, nums):
		ans = []
		m = len(nums)
		nums.sort()

		def recursion(idx, ds):
			ans.append(ds.copy())
			for i in range(idx, m):
				if i > idx and nums[i] == nums[i - 1]:
					continue
				ds.append(nums[i])
				recursion(i + 1, ds)
				ds.remove(nums[i])
		recursion(0, [])
		return ans


class Solution:

	def solve(self, i, nums, lst, ans):
		if i == n:
			ans.add(tuple(lst))
			return
		lst.append(nums[i])
		self.solve(i + 1, nums, lst, ans)
		lst.pop()
		self.solve(i + 1, nums, lst, ans)

	def printUniqueSubset(self, nums):
		nums.sort()
		ans = set()
		lst = []
		i = 0
		self.solve(i, nums, lst, ans)
		return sorted(ans)