<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 dfs(vector<vector<int>> curr, unordered_map<int, int> m, vector<vector<int>>& res, int& m_size)
    {
        vector<vector<int>> next;
        for(auto perm : curr)
        {
            for(auto itr:m)
            {
                if(itr.second != 0)
                {
                    vector<int> new_perm = perm;
                    new_perm.push_back(itr.first);
                    if(new_perm.size() == m_size)
                        res.push_back(new_perm);
                    next.push_back(new_perm);
                    m[itr.first] = m[itr.first] - 1;
                    dfs(next, m, res, m_size);
                    m[itr.first] = m[itr.first] + 1;
                    next.pop_back();
                }
            }
        }
        
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) 
    {
        unordered_map<int, int> m;
        for(auto num : nums)
        {
            if(m.find(num) == m.end())
                m[num] = 1;
            else
                m[num] = m[num]+1;
        }
        vector<vector<int>> curr;
        curr.push_back(vector<int>());
        vector<vector<int>> res;
        int m_size = nums.size();
        dfs(curr, m, res, m_size);
        return res;
    }
};
23180