private static void combinationSumHelper2(int[] candidates, int target, int index, 
                            ArrayList<Integer> solution, ArrayList<ArrayList<Integer>> result) {
        if (target == 0) {
           result.add(new ArrayList<Integer>(solution));
           return;
        }
        else if (index == candidates.length) {
            return;
        }
        
        int num = 0, i = index;
        while (i < candidates.length && candidates[i] == candidates[index]) {
            i++; num++;
        }
        
        for (i = 0; i <= Math.min(num, (int)(target / candidates[index])); i++) {
            for (int j = 0; j < i; j++) {
                solution.add(candidates[index]);
            }
            combinationSumHelper2(candidates, target - candidates[index] * i, index + num, solution ,result);
            for (int j = 0; j < i; j++) {
                solution.remove(solution.size() - 1);
            }
        }
    }