<p>Given a collection of numbers, <code>nums</code>,&nbsp;that might contain duplicates, return <em>all possible unique permutations <strong>in any order</strong>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,2]
<strong>Output:</strong>
[[1,1,2],
 [1,2,1],
 [2,1,1]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 8</code></li>
	<li><code>-10 &lt;= nums[i] &lt;= 10</code></li>
</ul>

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        res = []
        count = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return 
            for j in count:
                if count[j]>0:
                    cur.append(j)
                    count[j]-=1
                    dfs(cur)
                    count[j]+=1
                    cur.pop()
        dfs([])
        return res













        res = []
        count = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for j in count:
                if count[j]>0:
                    count[j]-=1
                    cur.append(j)
                    dfs(cur)
                    cur.pop()
                    count[j]+=1
        dfs([])
        return res
















        res = []
        count = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for j in count:
                if count[j]>0:
                    count[j]-=1
                    cur.append(j)
                    dfs(cur)
                    cur.pop()
                    count[j]+=1
        dfs([])
        return res
















        res = []
        count = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return 
            for j in count:
                if count[j]>0:
                    count[j]-=1
                    cur.append(j)
                    dfs(cur)
                    cur.pop()
                    count[j]+=1
        dfs([])
        return res












        res = []
        counter = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]>0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(cur)
                    cur.pop()
                    counter[j]+=1
        dfs([])
        return res




















        res = []
        counter = Counter(nums)
        def dfs(i,cur):
            if i>=len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]!=0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(i+1,cur)
                    cur.pop()
                    counter[j]+=1
        dfs(0,[])
        return res





















        res = []
        counter = Counter(nums)
        def dfs(i,cur):
            if i>=len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]!=0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(i+1,cur)
                    cur.pop()
                    counter[j]+=1
        dfs(0,[])
        return res
            



















        
        res = []
        # nums.sort()
        count = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for j in count:
                if count[j]==0:continue
                count[j]-=1
                cur.append(j)
                dfs(cur)
                cur.pop()
                count[j]+=1
        dfs([])
        return res






















        res = []
        count = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for  j in count:
                if count[j]==0:continue
                count[j]-=1
                cur.append(j)
                dfs(cur)
                cur.pop()
                count[j]+=1
        dfs([])
        return res
























        counter = Counter(nums)
        res = []
        def dfs(cur):
            if len(cur)==len(nums):
                res.append(cur.copy())
                return res
            for j in counter:
                if counter[j]>0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(cur)
                    cur.pop()
                    counter[j]+=1
        dfs([])
        return res




























        counter = Counter(nums)
        res = []
        def dfs(cur):
            if len(cur)==len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]>0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(cur)
                    counter[j]+=1
                    cur.pop()

        dfs([])
        return res






























        counter = Counter(nums)
        res = []
        def dfs(i,cur):
            if i>=len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]!=0:
                    cur.append(j)
                    counter[j]-=1
                    dfs(i+1,cur)
                    counter[j]+=1
                    cur.pop()
        dfs(0,[])
        return res
































        counter = Counter(nums)
        res = []
        def dfs(i,cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            if i>=len(nums):return
            for j in  counter:
                if counter[j]>0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(i+1,cur)
                    cur.pop()
                    counter[j]+=1
        dfs(0,[])
        return res


































        counter = Counter(nums)
        res = []
        def dfs(i,cur):
            if len(cur)==len(nums):
                res.append(cur.copy())
                return
            if i>=len(nums):
                return
            for j in counter:
                if counter[j]>0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(i+1,cur)
                    cur.pop()
                    counter[j]+=1
        dfs(0,[])
        return res
                    






























        counter = Counter(nums)
        res = []
        def dfs(i,cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            if i>=len(nums):return
            for j in counter:
                if counter[j]>0:
                    counter[j] -=1
                    cur.append(j)
                    dfs(i+1,cur)
                    cur.pop()
                    counter[j]+=1
        dfs(0,[])
        return res
        






































        counter = Counter(nums)
        res = []
        def dfs(i,cur):
            if len(cur) == len(nums):
                
                res.append(cur.copy())
                return
            if  i>=len(nums):return
            for j in counter:
                if counter[j] > 0:
                    counter[j]-=1
                    cur.append(j)
                    dfs(i+1,cur)
                    counter[j]+=1
                    cur.pop()
                    
        dfs(0,[])
        return res

































        counter = Counter(nums)
        res = []
        def dfs(i,cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]>0:
                    cur.append(j)
                    counter[j]-=1
                    dfs(i+1,cur)
                    counter[j]+=1
                    cur.pop()
        dfs(0,[])
        return res




































        res = []
        counter = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]>0:
                    cur.append(j)
                    counter[j]-=1
                    dfs(cur)
                    counter[j]+=1
                    cur.pop()
        dfs([])
        return res

































        res = []
        counter = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for j in counter:
                if counter[j]>0:
                    cur.append(j)
                    counter[j]-=1
                    dfs(cur)
                    counter[j]+=1
                    cur.pop()
        dfs([])
        return res
                    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        res=[]
        count = Counter(nums)
        def dfs(cur):
            if len(cur) == len(nums):
                res.append(cur.copy())
                return
            for n in count:
                if count[n] >0:
                    cur.append(n)
                    count[n]-=1

                    dfs(cur)

                    count[n]+=1
                    cur.pop()
        dfs([])
        return res
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        def perm(not_so_far,so_far):
            if len(so_far)==len(nums):
                res.append(so_far)
                return
            for i in set(not_so_far):
                not_so_far_t=not_so_far.copy()
                so_far_t=so_far.copy()
                not_so_far_t.remove(i)
                so_far_t.append(i)
                perm(not_so_far_t,so_far_t)
        perm(nums,[])
        return res
        
17700