I've used slightly modified code for Permutations task with the following additions:

Suppose that we have the following array

[0_a;0_b;1;1]

for correct solution we must ensure that items of the same value shouldn't switch places in permutation.

Basically it means that any of the following is valid

0_a; 1; 0_b; 1

1;0_a;0_b;1

etc

but this not

0_b;0_a;1;1

for enforcing that rule I'm doing simple cycle that checks the following condition:

If in left part of array there is element with the same value that isn't used in permutation, it means that we trying to broke invariant. Skip this permutation path.

The code is simple (~75% is reused from basic permutation task), but I still not sure that this is the best way to solve that task.

```
public class Permutations2 {
private boolean[] marked;
private int[] num;
private Integer[] currentPermutation;
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] num) {
marked = new boolean[num.length];
currentPermutation = new Integer[num.length];
this.num = num;
Arrays.sort(num);
permute(0);
return new ArrayList<>(result);
}
private void permute(int currentIndex) {
if(currentIndex==num.length) {
// add the result
result.add(new ArrayList<>(Arrays.asList(currentPermutation)));
return;
}
for (int i = 0; i < num.length; i++) {
// If in left there is an element with the same value but not used in permutation
// it means that we have case when we 1(at0) 1(at1) try to put 1(at1) before 1(0), and
// it will generate duplicates.
// so there is simple check for that case by iterating
if(!marked[i]) {
boolean hasLessNotMarked = false;
int j = i-1;
while (j>=0 && num[i]==num[j]) {
if(!marked[j]) {
hasLessNotMarked = true;
}
j--;
}
if(!hasLessNotMarked) {
marked[i] = true;
currentPermutation[currentIndex] = num[i];
permute(currentIndex + 1);
marked[i] = false;
}
}
}
}
}
```