Skip to content
This repository was archived by the owner on Nov 11, 2021. It is now read-only.
This repository was archived by the owner on Nov 11, 2021. It is now read-only.

PermutationDemo generates duplicate #8

Description

@skills-up

The PermutationDemo.java program will lead to duplicates. A quick fix is to do the inner loop on the clone of the array.

    public static void printPermutn(int[] arr, int index) {
        int len = arr.length;

        if (index == (len-1)) {
            for(int i=0; i<len; i++) System.out.print(arr[i]);
            System.out.println();
        } else {
            int[] a = arr.clone();
            for(int i=index; i<len; i++) {
                int t = a[i];
                a[i] = a[index];
                a[index] = t;
                printPermutn(a, index+1);
            }
        }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions