Making Permutation and Combinations in Alphabetic order (Javascript)

Viewed 37

Problem Statement:

I have list of Alphabets array: const Alphabets = ['a','b','c','d','e','f','g','h','i','j',''k','l','m','n','o','p','q','r','s','t','u','v','w','x','y',z];

I want to have list list of unique alphabetic combinations from one digit till 2 digits. For example :

const result = ['a','b','c','d','e','f','g','h','i','j',''k','l','m','n','o','p','q','r','s','t','u','v','w','x','y',z', 'aa','ab','ac ..... , 'ba','bb','bc','bd'......., 'ca','cb',....till zz];

Also there should not be any duplicate values. Please assist

2 Answers
const alphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
function combo(arr) {
  const combinations = new Set();
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length; j++) {
      combinations.add(arr[i] + arr[j]);
    }
  }
  return combinations;
}
console.log(combo(alphabets));
let a=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
str=[]
for(let i of a){
  for(let j of a){
    str.push(i+j)
  }
}
document.write(str)
Related