Permutation { public static <T> List<List<T>> insertPermutateNoDup(T[] array) { List<List<T>> list = new ArrayList<>(); list.add(new ArrayList<>()); for (int i = 0; i < array.length; i++) { T t = array[i]; List<List<T>> next = new ArrayList<>(); for (List<T> l : list) { for (int j = 0; j < l.size() + 1; j++) { l.add(j, t); next.add(new ArrayList<>(l)); l.remove(j); } } list = new ArrayList<>(next); } return list; }  static List<List<T>> insertPermutateNoDup(T[] array); static List<List<T>> recursivePermutateNoDup(T[] array); static void recursivePermutateNoDup(T[] array, int start, List<List<T>> cache); static List<List<T>> permuteDup(T[] array); List<List<String>> allChoices(List<String[]> categories);  }
@Test public void testInsertPermutateNoDup() { Integer[] array = new Integer[]{1, 2, 3}; List<List<Integer>> result = Permutation.insertPermutateNoDup(array); TestUtilities.assertEquals(result, Arrays.asList(Arrays.asList(3, 2, 1), Arrays.asList(2, 3, 1), Arrays.asList(2, 1, 3), Arrays.asList(3, 1, 2), Arrays.asList(1, 3, 2), Arrays.asList(1, 2, 3))); }