3

I am trying to combine items in an array, with every item below it. It should make a set of the current character and each character below it, and iteratively walk down the array. For example, if I have an array like this:

var myArray = ['A','B','C','D']

I would like an output like this:

AB AC AD BC BD CD

The code I have is getting me close, but I am having a hard time figuring out the rest. Here is what I have so far:

var myArray = ['A', 'B', 'C', 'D']
var sql_parts = []
var string = "";
for (var i = 0; i < myArray.length; i++) {
  recurse_function(string, i)
}
console.log(sql_parts)

function recurse_function(string_val, count) {
  if ((myArray.length - count) == 0) {
    return string_val;
  } else {
    string_val += myArray[count]
    sql_parts.push(string_val)
    recurse_function(string_val, count + 1)
  }
}

But this produces:

["A", "AB", "ABC", "ABCD", "B", "BC", "BCD", "C", "CD", "D"]

customcommander
  • 17,580
  • 5
  • 58
  • 84
jason
  • 3,821
  • 10
  • 63
  • 120
  • Where do you specify that the resulting strings should only be 2 characters long? – Barmar Aug 29 '21 at 16:52
  • @Barmar Thanks for the clarifying question. I will edit the original question to reflect that requirement – jason Aug 29 '21 at 16:54

5 Answers5

1

Here is one solution:

  • Define the recursive function to take the array and an empty list initially to store the combinations
  • The base condition is when the array is empty or has one element
  • Otherwise, Remove the first element "start"
  • Iterate over the array to store its combinations with its following elements
  • Recur again with array and combinations updated

function recurse_function(array, combinations = []) {
  if(array.length <= 1) return combinations;
  const start = array.shift();
  for(let i = 0; i < array.length; i++) combinations.push(`${start}${array[i]}`);
  return recurse_function(array, combinations);
}

console.log( recurse_function(['A','B','C','D']) );
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1
var myArray = ['A','B','C','D']
var sql_parts = []
for(var i =0; i< myArray.length; i++){
  var a = myArray[i]
  for(var j = i+1; j<myArray.length; j++){
    var b=myArray[j]
    var c= a+b
    sql_parts.push(c)
  }
}
Subhashis Pandey
  • 1,473
  • 1
  • 13
  • 16
1

I think this is something that you're looking for, it's a function that is very cheap on resources. Its not recursive (why you now would need something like that for this simple scenario)

var myArray = ['A','B','C','D']
let [a, b, iter] = [0, 1, 2]
let result = []

for (;a < myArray.length; a++) {
  for (;b < myArray.length; b++) {
      result.push(myArray[a]+myArray[b])
  } 
  b = iter++
}

console.log(result)
t.deeeerp
  • 31
  • 4
1

I don't think you need to recurse in this particular case. You can simply combine the current element with the following ones with flatMap:

['A','B','C','D'].flatMap((x, i, xs) => xs.slice(i+1).map(y => x+y));
//=> ["AB", "AC", "AD", "BC", "BD", "CD"]
customcommander
  • 17,580
  • 5
  • 58
  • 84
0

var myArray = ['A', 'B', 'C', 'D'];
var result = [];
myArray.forEach((item, index) => {
  myArray.forEach((item2, index2) => (index < index2 ? result.push(item + item2) : ''));
});
console.log(result);
Pramod Kumar
  • 96
  • 1
  • 2
  • 7