1

This is my first question here, I think the answer will be a small piece of Code.

Lets say i have a filled array at any lenght with unique elements:

let a = [A,B,C]

My question is, How can I process the array, so that I have a list or x arrays , that show every possible order for these elements of the array above like:

A,B,C
A,C,B
B,A,C
B,C,A
C,B,A
C,A,B

I dont really have an idea and tried to look in the web so far.

Thanks for the answers!

twenseyofu
  • 11
  • 2

1 Answers1

0

The recursive idea is:

function order(list, ans){
    let size = list.length;
    if(size == 0){
        console.log(ans);
        return;
    }
    for(var i = 0; i < size; i++){
        let first = list.shift();
        order(list, ans + `${first},`);
        list.push(first);
    }
}

Basically, for each element, you set it as a first and recursively do the same thing with the rest of the list.

Daniel
  • 7,357
  • 7
  • 32
  • 84