> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/solutions/permutations_ii/java.md).

# Java

public class Solution {

public List\<List\<Integer>> permuteUnique(int\[] num) {

List\<List\<Integer>> ret= new ArrayList\<List\<Integer>>();

List\<Integer> s=new ArrayList\<Integer>();

if(num==null||num.length==0)

return ret;

Arrays.sort(num);

dfs(ret, s, num, new boolean\[num.length]);

return ret;

}

void dfs(List\<List\<Integer>> ret, List\<Integer> s, int \[] num, boolean \[] used){

if(s.size()==num.length){

ret.add(new ArrayList(s));

return;

}

for(int i=0;i\<num.length;i++){

if(i>0&&!used\[i-1]&\&num\[i-1]==num\[i]) continue;

if(!used\[i]){

used\[i]=true;

s.add(num\[i]);

dfs(ret,s,num,used);

used\[i]=false;

s.remove(s.size()-1);

}

}

}

}
