<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 {
public:
    void solve(unordered_map<int,int>& m,vector<vector<int>>& ans,vector<int>contri,int& n){
        if(contri.size()==n){
            ans.push_back(contri);
            return ;
        }
        for(auto it=m.begin();it!=m.end();it++){
            if(it->second >0){
                m[it->first]-=1;
                // cout<<it->first<<" "<<it->second<<endl;
                vector<int>temp=contri;
                contri.push_back(it->first);
                solve(m,ans,contri,n);
                m[it->first]+=1;
                contri=temp;
            }
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        int n=nums.size();
        sort(nums.begin(),nums.end());
        unordered_map<int,int>m;
        for(int i=0;i<n;i++){
            m[nums[i]]+=1;
        }
        // for(auto it=m.begin();it!=m.end();it++){
        //     cout<<it->first<<" "<<it->second<<endl;
        // }
        vector<vector<int>>ans;
        solve(m,ans,{},n);
        return ans;
    }
};
14833