private void permuteUniqueHelper(int[] num, ArrayList<Integer> solution, ArrayList<ArrayList<Integer>> result, boolean[] visited) {
        int index = 0;
        while (index < num.length) {
            if (visited[index]) {
                continue;
            }
            solution.add(num[index]);
            visited[index] = true;
            if (solution.size() == num.length) {
                result.add(new ArrayList<Integer>(solution));
            }
            else {
                permuteUniqueHelper(num, solution, result, visited);
            }
            solution.remove(solution.size() - 1);
            visited[index] = false;
            
            while (index < num.length - 1 && visited[index + 1] == visited[index]) {
                index++;
            }
        }
    }