<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:
    vector<vector<int>>solve(map<int, int>mp, vector<int> cur, int n){
        vector<vector<int>>res;
        if(cur.size() == n){
            res.push_back(cur);
            return res;
        }

        for(auto x: mp){
            if(x.second == 0)continue;
            int val = x.first;
            cur.push_back(val);
            mp[val]--;
            auto cur_res = solve(mp, cur, n);
            for(auto v: cur_res){
                vector<int>vcopy = v;
                v.push_back(val);
                res.push_back(vcopy);
            }
            cur.pop_back();
            mp[val]++;
        }
        return res;
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        map<int, int>mp;
        for(int i: nums)mp[i]++;
        vector<int>v;
        return solve(mp, v, nums.size());
    }
};
28281