Related
How could this one be tweaked so that it could increment a set of two letters, so that it'd look like this:
AA, AB, AC...AZ, BA, BB, BC, etc
This is borrowed from tckmn, but it addresses one letter only.
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
function incrementChar(c) {
var index = alphabet.indexOf(c)
if (index == -1) return -1 // or whatever error value you want
return alphabet[index + 1 % alphabet.length]
}
Appreciate your help!
You just need two loops. One to iterate over the alphabet, and the second to iterate over the alphabet on each iteration of the first loop.
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const arr = [];
for (let i = 0; i < alphabet.length; i++) {
for (let j = 0; j < alphabet.length; j++) {
arr.push(`${alphabet[i]}${alphabet[j]}`);
}
}
console.log(arr);
In your situation, how about the following sample script?
const increase = s => {
const idx = [...s].reduce((c, e, i, a) => (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)), -1);
// Ref: https://stackoverflow.com/a/53678158
columnIndexToLetter = (n) => (a = Math.floor(n / 26)) >= 0 ? columnIndexToLetter(a - 1) + String.fromCharCode(65 + (n % 26)) : "";
return columnIndexToLetter(idx + 1);
};
const samples = ["A", "Z", "AA", "AZ", "ZZ"];
const res1 = samples.map(e => increase(e));
console.log(res1); // <--- [ 'B', 'AA', 'AB', 'BA', 'AAA' ]
// If you want to give one value, please use the following script.
const sample = "AA";
const res2 = increase(sample);
console.log(res2); // <--- AB
When this script is run, ["A", "Z", "AA", "AZ", "ZZ"] is converted to [ 'B', 'AA', 'AB', 'BA', 'AAA' ].
Reference:
map()
I have this array in JS
initialArray = [A,B,C,C,D,E,F,F,G,K]
I want to split into:
chucks = [[A,B,C], [C,D,E,F], [F,G,K]]
Duplicate items are separators such as 'C' or 'F'
How to do this split in ES6?
You could reduce the array and add either the value or a new array, depending on the last value.
const
array = ['A','B','C','C','D','E','F','F','G','K'],
result = array.reduce((r, v, i, { [i - 1]: last }) => {
if (v === last) r.push([]);
r[r.length - 1].push(v);
return r;
}, [[]]);
console.log(result);
Fairly straight forward solution using a single for loop and holding the current chunk in a variable to push to.
const initialArray = ['A', 'A', 'B', 'C', 'C', 'D', 'E', 'F', 'F', 'G', 'K', 'K'];
const chunk_at_duplicate = (arr) => {
let chunk = [], res = [chunk];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === arr[i - 1]) {
res.push(chunk = []);
}
chunk.push(arr[i]);
}
return res;
};
console.log(chunk_at_duplicate(initialArray));
Edited: now is based on clones
const initialArray = ["A","B","C","C","D","E","F","F","G","K"];
const chunks = [];
let smallChunk = [];
let index = 0;
while (index < initialArray.length){
smallChunk.push(initialArray[index]);
if (initialArray[index+1] == initialArray[index]){
chunks.push(smallChunk);
smallChunk = [];
}
index++;
}
chunks.push(smallChunk);
console.log(chunks);
You can use for ... of together with initialArray.entries() as follows:
const initialArray = ["A","B","C","C","D","E","F","F","G","K"];
const chunks = [];
let start = 0
for(const [i,v] of initialArray.entries()) {
if(i > 0 && v === initialArray[i-1]) {
chunks.push( initialArray.slice(start,i) );
start = i;
} else if( i === initialArray.length - 1 ) {
chunks.push( initialArray.slice(start) );
}
}
console.log( chunks );
I have an array with 7 items in it. I want to move a few items to a different index in the same order as it was in the original array. I have pasted in code snippet on whatever I tried so far.
let originalArray = ['a','b','c','d','e','f','g'];
let itemsToBeMoved = ['c','f','e'];
let newIndexToBeMoved = 4;
//expected result is ['a','b','d','c','e','f','g'];
let movableItemsIndex = [];
movableItemsIndex.push(originalArray.indexOf('c'));
movableItemsIndex.push(originalArray.indexOf('f'));
movableItemsIndex.push(originalArray.indexOf('e'));
//To be Moved items has to be sorted as in originalArray
movableItemsIndex.sort();
let itemsToBeMovedSorted = [originalArray[movableItemsIndex[0]],originalArray[movableItemsIndex[1]],originalArray[movableItemsIndex[2]]];
//Removing items before inserting to items to new position
while(movableItemsIndex.length) {
originalArray.splice(movableItemsIndex.pop(), 1);
}
let newUpdatedArray = [...originalArray],j=0;
for(let i = newIndexToBeMoved ;i < originalArray.length ; i++){
newUpdatedArray[i] = itemsToBeMovedSorted[j];
j++;
}
console.log(newUpdatedArray);
Assuming that all elements are unique:
let originalArray = ['a','b','c','d','e','f','g'];
let itemsToBeMoved = ['c','f','e'];
let newIndexToBeMoved = 4;
// find the value of the element the marks the insertion point
let insertBefore = originalArray[newIndexToBeMoved];
// in original sequence order, check for presence in the removal
// list, *and* remove them from the original array
let moved = [];
for (let i = 0; i < originalArray.length; ) {
let value = originalArray[i];
if (itemsToBeMoved.indexOf(value) >= 0) {
moved.push(value);
originalArray.splice(i, 1);
} else {
++i;
}
}
// find the new index of the insertion point
let insertionIndex = originalArray.indexOf(insertBefore);
if (insertionIndex < 0) {
insertionIndex = originalArray.length;
}
// and add the elements back in
originalArray.splice(insertionIndex, 0, ...moved);
console.log(originalArray);
This can be solved many ways, but here is a quick run-down of what could be done:
First, you could create a "present" map (based on the positions in the source array) for the items to be moved
Now, filter the array, removing the items, but keeping track whether they are present
Splice the (sorted) present, filtered-out, values into the altered array at the desired position (based on the index in the source array)
const move = (arr, items, index) => {
const present = new Map(items.map(item => [item, arr.indexOf(item)]));
const altered = arr.filter(item => !present.has(item));
altered.splice(arr.indexOf(arr[index - 1]), 0, ...([...present.entries()]
.filter(([k, v]) => v !== -1)
.sort(([, k1], [, k2]) => k1 - k2)
.map(([k, v]) => k)));
return altered;
}
const moved = move(['a','b','c','d','e','f','g'], ['c','f','e'], 4);
console.log(moved);
.as-console-wrapper { top: 0; max-height: 100% !important; }
Here's a way to do it with typescript and generics. It does not mutate the array, it's very fast (used the conclusions of this article to make it).
When moving items to a new position, there is always a range of items affected by the move. By affected, I mean that within that range, all items will have to move. I calculate the min/max of the range, any items outside that range will simply be copied from the old to new array.
Then, depending whether the items are moved up or down in the array, I copy each items from old to new array taking into account the offset of the move.
export const arrayMove = <ItemType>({
arr,
from,
to,
movedItemsCount = 1,
}: {
arr: ItemType[]
from: number
to: number
movedItemsCount?: number
}) => {
if (from === to) return arr
const minAffected = Math.min(from, to)
const maxAffected = Math.max(to, from + movedItemsCount - 1)
const pushedItemsCount = maxAffected - minAffected + 1 - movedItemsCount
const newArr: ItemType[] = []
newArr.length = arr.length
for (let i = 0; i < arr.length; i++) {
if (i < minAffected || i > maxAffected) {
// When i is outside the affected range,
// items are identical in new and old arrays
newArr[i] = arr[i]
} else {
// Moving items down
if (to > from) {
if (i < to - movedItemsCount + 1) {
// Write pushed items
newArr[i] = arr[i + movedItemsCount]
} else {
// Write moved items
newArr[i] = arr[i - pushedItemsCount]
}
} else {
// Moving items up
if (i < to + movedItemsCount) {
// Write moved items
newArr[i] = arr[i + pushedItemsCount]
} else {
// Write pushed items
newArr[i] = arr[i - movedItemsCount]
}
}
}
}
return newArr
}
Try this:
const originalArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const itemsToBeMoved = ['c', 'f', 'e'];
const newIndexToBeMoved = 4;
originalArray.splice(newIndexToBeMoved, 0, itemsToBeMoved)
console.log(originalArray.flat())
this is the problem description:
Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line.
for example given the array arr=[1,1,0,-1,-1] output should be:
0.400000
0.400000
0.200000
I know there is more more simple solution for it ,and i am sorry for my silly simple question but i wanna make my code work, my code sorts the output based on the key and removes duplicates. for this arr, my code output is:
0.200000
0.400000
thank you so much in advance for any help.
function plusMinus(arr) {
var freq = {};
for (var i = 0; i < arr.length; i++){
if (freq[arr[i]]) {
freq[arr[i]]++;
} else {
freq[arr[i]] = 1;
}
} for(var key in freq){
console.log((freq[key]/arr.length).toFixed(6));
}
}
You could take an object with predifined properties, this prevents each loop for checking the existence and take an array of keys for getting the result in a wanted order.
function plusMinus(arr) {
var freq = { 1: 0, '-1': 0, 0: 0 },
i, key;
for (i = 0; i < arr.length; i++) {
freq[arr[i]]++;
}
for (key of [1, -1, 0]) {
console.log((freq[key] / arr.length).toFixed(6));
}
}
plusMinus([1, 1, 0, -1, -1]);
Let's make sure the order of key in the map by defining it first.
function plusMinus(arr) {
var freq = {
posetive: 0,
negative: 0,
zero: 0
};
for (var i = 0; i < arr.length; i++){
if( arr[i] < 0) {
freq.negative++;
} else if(arr[i] > 0) {
freq.posetive++;
} else {
freq.zero++;
}
}
for(var key in freq){
console.log((freq[key]/arr.length).toFixed(6));
}
}
plusMinus([1,1,0,-1,-1]);
You can use reduce.
Here idea is
First loop through original array and check for the value.
If value is zero we increment count of zero key.
If value is positive we increment count of pos key.
If value is negative we increment count of neg key.
Finally we divide each count by length of array.
let arr = [1,1,0,-1,-1]
let op = arr.reduce((op,inp)=>{
if(inp === 0){
op.zero.count++
} else if (inp > 0){
op.pos.count++;
} else {
op.neg.count++;
}
return op
},{zero:{count:0},pos:{count:0},neg:{count:0}})
let final = Object.entries(op).map(([key,value])=>({
[key] : value.count / arr.length
}))
console.log(final)
Use reduce, map and filter:
const arr = [1, 1, 0, -1, -1];
const counts = arr.reduce((acc, curr) => {
if (!curr) acc[0]++;
else if (curr > 0) acc[1]++;
else acc[2]++;
return acc
}, [0, 0, 0]);
const result = counts.map(e => e / arr.length).filter((e, i, a) => a.indexOf(e) == i);
console.log(result);
You can try using Array.reduce and the resulting array will have the fraction of positive number at the '0'th index, negative at '1'st and zero at the '2'nd index.
Now if you want to control the count of the number of elements after decimal point, use Array.map at the end to transform it.
const array = [1,1,0,-1,-1];
function plusMinus(arr){
const output = arr.reduce((acc, ele) => {
if(ele > 0){
acc[0] = ((acc[0] || 0 ) + 1 / arr.length);
}
if(ele < 0){
acc[1] = ((acc[1] || 0 ) + 1 / arr.length);
}
if(ele === 0) {
acc[2] = ((acc[2] || 0 ) + 1 / arr.length);
}
return acc;
}, []).map(ele => ele.toFixed(6));
console.log(...output);
}
plusMinus(array);
Math.sign is your friend here.
Math.sign
Also lodash would really help this snippet to be cleaner, I highly recommend _.countBy. Lodash .countBy
Here's the code.
const plusMinus = (numbers) => {
// Count by Sign (-1, 0 1)
const countSign = _.countBy(numbers, Math.sign);
// _.countBy return object, of counted { '1': 2, '0': 1, '-1': 2 }
// Print them in orders
const printOrder = [1, -1, 0];
printOrder.forEach(sign => {
console.log((countSign[sign] / numbers.length).toFixed(6));
});
}
const testArr = [1,1,0,-1,-1];
plusMinus(testArr);
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.11/lodash.min.js"></script>
Here is another one-line solution using Array.reduce() and Array.forEach() functions:
const plusMinus = arr => arr
.reduce((res, curr) => ++res[!curr ? 2 : curr < 0 ? 1 : 0] && res, [0, 0, 0])
.forEach(i => console.log((i / arr.length).toFixed(6)));
plusMinus([1, 1, 0, -1, -1]);
I've seen several similar questions about how to generate all possible combinations of elements in an array. But I'm having a very hard time figuring out how to write an algorithm that will only output combination pairs. Any suggestions would be super appreciated!
Starting with the following array (with N elements):
var array = ["apple", "banana", "lemon", "mango"];
And getting the following result:
var result = [
"apple banana"
"apple lemon"
"apple mango"
"banana lemon"
"banana mango"
"lemon mango"
];
I was trying out the following approach but this results in all possible combinations, instead only combination pairs.
var letters = splSentences;
var combi = [];
var temp= "";
var letLen = Math.pow(2, letters.length);
for (var i = 0; i < letLen ; i++){
temp= "";
for (var j=0;j<letters.length;j++) {
if ((i & Math.pow(2,j))){
temp += letters[j]+ " "
}
}
if (temp !== "") {
combi.push(temp);
}
}
Here are some functional programming solutions:
Using EcmaScript2019's flatMap:
var array = ["apple", "banana", "lemon", "mango"];
var result = array.flatMap(
(v, i) => array.slice(i+1).map( w => v + ' ' + w )
);
console.log(result);
Before the introduction of flatMap (my answer in 2017), you would go for reduce or [].concat(...) in order to flatten the array:
var array = ["apple", "banana", "lemon", "mango"];
var result = array.reduce( (acc, v, i) =>
acc.concat(array.slice(i+1).map( w => v + ' ' + w )),
[]);
console.log(result);
Or:
var array = ["apple", "banana", "lemon", "mango"];
var result = [].concat(...array.map(
(v, i) => array.slice(i+1).map( w => v + ' ' + w ))
);
console.log(result);
A simple way would be to do a double for loop over the array where you skip the first i elements in the second loop.
let array = ["apple", "banana", "lemon", "mango"];
let results = [];
// Since you only want pairs, there's no reason
// to iterate over the last element directly
for (let i = 0; i < array.length - 1; i++) {
// This is where you'll capture that last value
for (let j = i + 1; j < array.length; j++) {
results.push(`${array[i]} ${array[j]}`);
}
}
console.log(results);
Rewritten with ES5:
var array = ["apple", "banana", "lemon", "mango"];
var results = [];
// Since you only want pairs, there's no reason
// to iterate over the last element directly
for (var i = 0; i < array.length - 1; i++) {
// This is where you'll capture that last value
for (var j = i + 1; j < array.length; j++) {
results.push(array[i] + ' ' + array[j]);
}
}
console.log(results);
In my case, I wanted to get the combinations as follows, based on the size range of the array:
function getCombinations(valuesArray: String[])
{
var combi = [];
var temp = [];
var slent = Math.pow(2, valuesArray.length);
for (var i = 0; i < slent; i++)
{
temp = [];
for (var j = 0; j < valuesArray.length; j++)
{
if ((i & Math.pow(2, j)))
{
temp.push(valuesArray[j]);
}
}
if (temp.length > 0)
{
combi.push(temp);
}
}
combi.sort((a, b) => a.length - b.length);
console.log(combi.join("\n"));
return combi;
}
Example:
// variable "results" stores an array with arrays string type
let results = getCombinations(['apple', 'banana', 'lemon', ',mango']);
Output in console:
The function is based on the logic of the following documentation, more information in the following reference:
https://www.w3resource.com/javascript-exercises/javascript-function-exercise-3.php
if ((i & Math.pow(2, j)))
Each bit of the first value is compared with the second, it is taken as valid if it matches, otherwise it returns zero and the condition is not met.
Although solutions have been found, I post here an algorithm for general case to find all combinations size n of m (m>n) elements. In your case, we have n=2 and m=4.
const result = [];
result.length = 2; //n=2
function combine(input, len, start) {
if(len === 0) {
console.log( result.join(" ") ); //process here the result
return;
}
for (let i = start; i <= input.length - len; i++) {
result[result.length - len] = input[i];
combine(input, len-1, i+1 );
}
}
const array = ["apple", "banana", "lemon", "mango"];
combine( array, result.length, 0);
I ended up writing a general solution to this problem, which is functionally equivalent to nhnghia's answer, but I'm sharing it here as I think it's easier to read/follow and is also full of comments describing the algorithm.
/**
* Generate all combinations of an array.
* #param {Array} sourceArray - Array of input elements.
* #param {number} comboLength - Desired length of combinations.
* #return {Array} Array of combination arrays.
*/
function generateCombinations(sourceArray, comboLength) {
const sourceLength = sourceArray.length;
if (comboLength > sourceLength) return [];
const combos = []; // Stores valid combinations as they are generated.
// Accepts a partial combination, an index into sourceArray,
// and the number of elements required to be added to create a full-length combination.
// Called recursively to build combinations, adding subsequent elements at each call depth.
const makeNextCombos = (workingCombo, currentIndex, remainingCount) => {
const oneAwayFromComboLength = remainingCount == 1;
// For each element that remaines to be added to the working combination.
for (let sourceIndex = currentIndex; sourceIndex < sourceLength; sourceIndex++) {
// Get next (possibly partial) combination.
const next = [ ...workingCombo, sourceArray[sourceIndex] ];
if (oneAwayFromComboLength) {
// Combo of right length found, save it.
combos.push(next);
}
else {
// Otherwise go deeper to add more elements to the current partial combination.
makeNextCombos(next, sourceIndex + 1, remainingCount - 1);
}
}
}
makeNextCombos([], 0, comboLength);
return combos;
}
The best solutions I have found - https://lowrey.me/es6-javascript-combination-generator/
Uses ES6 generator functions, I adapted to TS. Most often you don't need all of the combinations at the same time. And I was getting annoyed by writing loops like for (let i=0; ... for let (j=i+1; ... for (let k=j+1... just to get combos one by one to test if I need to terminate the loops..
export function* combinations<T>(array: T[], length: number): IterableIterator<T[]> {
for (let i = 0; i < array.length; i++) {
if (length === 1) {
yield [array[i]];
} else {
const remaining = combinations(array.slice(i + 1, array.length), length - 1);
for (let next of remaining) {
yield [array[i], ...next];
}
}
}
}
usage:
for (const combo of combinations([1,2,3], 2)) {
console.log(combo)
}
output:
> (2) [1, 2]
> (2) [1, 3]
> (2) [2, 3]
Just to give an option for next who'll search it
const arr = ['a', 'b', 'c']
const combinations = ([head, ...tail]) => tail.length > 0 ? [...tail.map(tailValue => [head, tailValue]), ...combinations(tail)] : []
console.log(combinations(arr)) //[ [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'c' ] ]
There are also this answer:
https://stackoverflow.com/a/64414875/19518308
The alghorithm is this answer generates all the possible sets of combination(or choose(n, k)) of n items within k spaces.
The algorhitm:
function choose(arr, k, prefix=[]) {
if (k == 0) return [prefix];
return arr.flatMap((v, i) =>
choose(arr.slice(i+1), k-1, [...prefix, v])
);
}
console.log(choose([0,1,2,3,4], 3));
I had a similar problem and this algorhitm is working very well for me.
Using map and flatMap the following can be done (flatMap is only supported on chrome and firefox)
var array = ["apple", "banana", "lemon", "mango"]
array.flatMap(x => array.map(y => x !== y ? x + ' ' + y : null)).filter(x => x)
I think it is an answer to all such questions.
/**
*
* Generates all combination of given Array or number
*
* #param {Array | number} item - Item accepts array or number. If it is array exports all combination of items. If it is a number export all combination of the number
* #param {number} n - pow of the item, if given value is `n` it will be export max `n` item combination
* #param {boolean} filter - if it is true it will just export items which have got n items length. Otherwise export all posible length.
* #return {Array} Array of combination arrays.
*
* Usage Example:
*
* console.log(combination(['A', 'B', 'C', 'D'], 2, true)); // [[ 'A','A' ], [ 'A', 'B' ]...] (16 items)
* console.log(combination(['A', 'B', 'C', 'D'])); // [['A', 'A', 'A', 'B' ],.....,['A'],] (340 items)
* console.log(comination(4, 2)); // all posible values [[ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 0, 0 ], [ 0, 1 ], [ 0, 2 ]...] (20 items)
*/
function combination(item, n) {
const filter = typeof n !=='undefined';
n = n ? n : item.length;
const result = [];
const isArray = item.constructor.name === 'Array';
const count = isArray ? item.length : item;
const pow = (x, n, m = []) => {
if (n > 0) {
for (var i = 0; i < count; i++) {
const value = pow(x, n - 1, [...m, isArray ? item[i] : i]);
result.push(value);
}
}
return m;
}
pow(isArray ? item.length : item, n);
return filter ? result.filter(item => item.length == n) : result;
}
console.log("#####first sample: ", combination(['A', 'B', 'C', 'D'], 2)); // with filter
console.log("#####second sample: ", combination(['A', 'B', 'C', 'D'])); // without filter
console.log("#####third sample: ", combination(4, 2)); // gives array with index number
Generating combinations of elements in an array is a lot like counting in a numeral system,
where the base is the number of elements in your array (if you account for the leading zeros that will be missing).
This gives you all the indices to your array (concatenated):
arr = ["apple", "banana", "lemon", "mango"]
base = arr.length
idx = [...Array(Math.pow(base, base)).keys()].map(x => x.toString(base))
You are only interested in pairs of two, so restrict the range accordingly:
range = (from, to) = [...Array(to).keys()].map(el => el + from)
indices = range => range.map(x => x.toString(base).padStart(2,"0"))
indices( range( 0, Math.pow(base, 2))) // range starts at 0, single digits are zero-padded.
Now what's left to do is map indices to values.
As you don't want elements paired with themselves and order doesn't matter,
those need to be removed, before mapping to the final result.
const range = (from, to) => [...Array(to).keys()].map(el => el + from)
const combinations = arr => {
const base = arr.length
return range(0, Math.pow(base, 2))
.map(x => x.toString(base).padStart(2, "0"))
.filter(i => !i.match(/(\d)\1/) && i === i.split('').sort().join(''))
.map(i => arr[i[0]] + " " + arr[i[1]])
}
console.log(combinations(["apple", "banana", "lemon", "mango"]))
With more than ten elements, toString() will return letters for indices; also, this will only work with up to 36 Elements.
Generating combinations is a classic problem. Here's my interpretation of that solution:
const combinations = (elements) => {
if (elements.length == 1) {
return [elements];
} else {
const tail = combinations(elements.slice(1));
return tail.reduce(
(combos, combo) => { combos.push([elements[0], ...combo]); return combos; },
[[elements[0]], ...tail]
);
}
};
const array = ["apple", "banana", "lemon", "mango"];
console.log(combinations(array));
Here is an non-mutating ES6 approach combining things (TS):
function combine (tail: any[], length: number, head: any[][] = [[]]): any[][] {
return tail.reduce((acc, tailElement) => {
const tailHeadVariants = head.reduce((acc, headElement: any[]) => {
const combination = [...headElement, tailElement]
return [...acc, combination]
}, [])
if (length === 1) return [...acc, tailHeadVariants]
const subCombinations = combine(tail.filter(t => t !== tailElement), length - 1, tailHeadVariants)
return [...acc, ...subCombinations]
}, [])
}
As this post is well indexed on Google under the keywords "generate all combinations", lots of people coming here simply need to generate all the unique combinations, regardless of the size of the output (not only pairs).
This post answers this need.
All unique combinations, without recursion:
const getCombos = async (a) => {
const separator = '';
const o = Object();
for (let i = 0; i < a.length; ++i) {
for (let j = i + 1; j <= a.length; ++j) {
const left = a.slice(i, j);
const right = a.slice(j, a.length);
o[left.join(separator)] = 1;
for (let k = 0; k < right.length; ++k) {
o[[...left, right[k]].join(separator)] = 1;
}
}
}
return Object.keys(o);
}
const a = ['a', 'b', 'c', 'd'];
const b = await getCombos(a);
console.log(b);
// (14) ['a', 'ab', 'ac', 'ad', 'abc', 'abd', 'abcd',
// 'b', 'bc', 'bd', 'bcd', 'c', 'cd', 'd']
This code splits the array into 2 sub arrays, left / right, then iterate over the right array to combine it with the left array. The left becomes bigger overtime, while the right becomes smaller. The result has only unique values.
Beating a dead horse a bit, but with smaller sets where recursion limit and performance is not a problem, the general combination generation can be done recursively with "recurse combinations containing the first element in given array" plus "recurse combinations not containing the first element". It gives quite compact implementation as a generator:
// Generator yielding k-item combinations of array a
function* choose(a, k) {
if(a.length == k) yield a;
else if(k == 0) yield [];
else {
for(let rest of choose(a.slice(1), k-1)) yield [a[0], ...rest];
for(let rest of choose(a.slice(1), k)) yield rest;
}
}
And even slightly shorter (and twice faster, 1 M calls of 7 choose 5 took 3.9 seconds with my MacBook) with function returning and array of combinations:
// Return an array of combinations
function comb(a, k) {
if(a.length === k) return [a];
else if(k === 0) return [[]];
else return [...comb(a.slice(1), k-1).map(c => [a[0], ...c]),
...comb(a.slice(1), k)];
}