Published
Edited
Dec 10, 2021
1 star
Insert cell
# Permutations
Insert cell
function getPermutations (arr, numberToSelect) {
const permutations = [];
if (numberToSelect === 1) return arr.map((el) => [el]);

for (const [index, fixedVal] of Object.entries(arr)) {
const rest = [...arr.slice(0, +index), ...arr.slice(+index + 1)];
const childPermutations = getPermutations(rest, numberToSelect - 1);
const attached = childPermutations.map((el) => [fixedVal, ...el]);
permutations.push(...attached);
}

return permutations;
};
Insert cell
getPermutations([1, 2, 3, 4], 3);
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more