0

So I'm currently running into an issue: Storing all possible permutations in some data structure (so we don't want to store any repeats). This is my current implementation:

  public static void generatePerms(int[] arr, int start, List list){
    if(!list.contains(arr)){
    System.out.println(Arrays.toString(arr));
      list.add(arr);
    }
    for(int i = start; i < arr.length; i++){
      for(int j=0; j < arr.length; j++){
        int[] newArr = swap(i,j,arr);
        generatePerms(newArr, start+1, list);
      }
    }
  }

  public static int[] swap(int i, int j, int[] arr){
    int temp = arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    return arr.clone();
  }

public static void main(String[] args){
 generatePerms(new int[]{1,2,3}, 0, new ArrayList<int[]>());
}

Obviously we can use a set, I just used a List here for my own sake of needing to print -- so the contains() is particular to the fact that I am using a list rather than a set. My main issue: Is there a way where one can store the permutations in their array form {[2,3,1], [2,3,1], etc}? Or is this ultimately impossible and I would have to store them as a string 1,2,3 with explicit commas to distinguish the start and ending of a number and then iterate through each string, make an array and then store that. Another possible way is to spit out the repeats; but this probably requires some way of tracking them.

All in all, can arrays of permutations be stored in a data structure without repeats? The code above is Java.

chris123
  • 197
  • 3
  • 13
  • What language is this? The standard libraries of the language might be helpful here. – Zev Spitz Feb 19 '17 at 11:13
  • Possible duplicate of [How to Store unique objects to avoid the duplicates in java Set?](https://stackoverflow.com/questions/12776813/how-to-store-unique-objects-to-avoid-the-duplicates-in-java-set) – Paul Sweatte May 23 '17 at 23:24

0 Answers0