# Math behind leetcode problem 47 permutations II

Please tell me why the expression `i>0 && nums[i] == nums[i-1] && !used[i-1]` works on getting unique permutations. And what is the math behind it?
The problem is as follows:
Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example:

``Input: [1,1,2] Output: [   [1,1,2],   [1,2,1],   [2,1,1] ] ``

Here is the code:

``class Solution { public:     vector<vector<int>> permuteUnique(vector<int>& nums) {            vector<vector<int>> result;          vector<int> temp;         vector<bool> used(nums.size(), false);         sort(nums.begin(), nums.end());         backtrack(nums, temp, result, used);         return result;             }          void backtrack(vector<int>& nums, vector<int>& temp, vector<vector<int>>& result, vector<bool>& used){                  if(temp.size() == nums.size()){             result.emplace_back(temp);         }else{                          for(int i=0; i<nums.size(); i++){                 if(used[i] || i>0 && nums[i] == nums[i-1] && !used[i-1]) continue;                  used[i] = true;                 temp.push_back(nums[i]);                 backtrack(nums, temp, result, used);                 used[i] = false;                 temp.pop_back();             }                     }             }     }; ``