import java.util.*;

public class Solution {
    public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
        Arrays.sort(num);
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();
        ArrayList<Integer> child = new ArrayList<>();
        dfs(list,child,0,num,new boolean[num.length]);
        return list;
        
    }
    
    private void dfs(ArrayList<ArrayList<Integer>> list,ArrayList<Integer> child,int index,int[] num,boolean[] vis){
        if(index == num.length){
            list.add(new ArrayList<>(child));
            return ;
        }
        
        for(int i = 0; i < num.length; i++){
            if(i > 0 && num[i] == num[i-1] && !vis[i-1] || vis[i]) continue;
            child.add(num[i]);
            vis[i] = true;
            dfs(list,child,index + 1,num,vis);
            vis[i] = false;
            child.remove(index);
        }
    }
}