<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:
    unordered_set<int> memo;
public:

    void genKeyPushHashMap(vector<int>& permElem, vector<vector<int>>& permList) {
        int key=0;
        for(auto elem:permElem) {
            key=key*10+elem;            
        }
        if(memo.find(key)==memo.end()) {
            permList.push_back(permElem);
            memo.insert(key);
        }
    }

    void getAllPerm(vector<int>nums, int pos, vector<vector<int>>& permList) {

        if(pos>=nums.size())
            return;

        for(auto index=pos; index<nums.size(); index++) {
            for(auto subIndex=index+1; subIndex<nums.size();subIndex++) {
                vector<int> *permElem=new vector<int>(nums);
                if(nums[index]==nums[subIndex]) 
                    continue;
                swap((*permElem)[index], (*permElem)[subIndex]);                
                genKeyPushHashMap(*permElem, permList);
                getAllPerm(*permElem, index+1, permList);    
            }
        }


    }

    vector<vector<int>> permuteUnique(vector<int>& nums) {
        
        //ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
        vector<vector<int>> permList;
        permList.push_back(nums);
        getAllPerm(nums, 0, permList);
        
        return permList;
    }
};
19470