0

I have an array of strings array_strings and I want to get the maximum length of x consecutive values from the list and return a string. I have tried with this code but, its not working.

var myArr = ['we', 'make', 'the', 'best', 'dishes', 'in', 'cooking', 'class'];
x = 2;

function myFunc(array_strings, x) {
  // your code

  let val = '',
    y = 0;
  array_strings.sort((a, b) => {
    return a.length < b.length
  });
  if (array_strings.length > 0) {
    while (x) {
      val += array_strings[y];
      y++;
      x--;
    }
  }

  return val;
}
console.log(myFunc(myArr, x))
// expected output 'cookingclass'

on sorting, I cannot satisfy the consecutive order. Where am I going wrong?

JSmith
  • 4,519
  • 4
  • 29
  • 45
wanjd
  • 11
  • 4

4 Answers4

0

To keep it simple, first get all possible values for x consecutive items.

function longestConsecutives(arr, x) {
    var consecutives = [];
    for (var i=0; i<arr.length-x; i++)
        consecutives.push(arr.slice(i, x).join(''));

and then search the one with maximum length amongst them:

    var longest = consecutives[0];
    for (var i=1; i<consecutives.length; i++)
        if (consecutives[i].length > longest.length)
            longest = consecutives[i];
    return longest;
}

There is also a more efficient approach to find the result, have a look at this for inspiration.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

I don't think there's any reason to sort for this. You can just move a sliding window of x length over the array and keep track of the longest sequence you've seen:

let array_strings = ['we', 'make', 'the', 'best', 'dishes', 'in', 'cooking', 'class']


function findMax(array_strings, x){
    let max_index = 0, max_length = 0

    for (let i= 0; i < array_strings.length - x + 1; i++){
        let length = array_strings.slice(i, i+x).reduce((l, c) => l + c.length, 0)
        if (length > max_length) {
            max_index = i,
            max_length = length
        }
    }
    return array_strings.slice(max_index, max_index + x)
}
console.log(findMax(array_strings,2))
console.log(findMax(array_strings,3))
console.log(findMax(array_strings,4))
Mark
  • 90,562
  • 7
  • 108
  • 148
0

Rolling sum O(n) solution:

function findLongestConsec(arr, l) {
    var curSum = array_strings.slice(0, l).map(ele => ele.length).reduce((a, b) => a+b);
    var curMax = curSum;
    var curMaxIndex = 0;
    for (var i = 1; i < arr.length - l + 1; i++) {
        curSum = curMax - arr[i-1].length + arr[i+l-1].length;
        if (curSum > curMax) {
            curMax = curSum;
            curMaxIndex = i;
        }
    }
    return arr.slice(curMaxIndex, curMaxIndex + l).join('');
}

The previous solutions all involve string concatenation. I don't think it's worth doing it every time. We just have to keep track of the starting index of the longest subarray.

Kevin He
  • 1,210
  • 8
  • 19
0

Try this:

arr = ['we', 'make', 'the', 'best', 'dishes', 'in', 'cooking', 'class']
x = 2
var sorting = function (array_strings, items) {
    // your code

    let val = '', y = 0;
    const sort = array_strings.sort((a, b) => a.length < b.length);
    console.log({sort});
    
    if (arr.length > 0){
      while (items){
      val += array_strings[y];
      y++;
      items--;
     }
    }

 return val;
}

var s = sorting(arr,x);
console.log({s});
pakman198
  • 123
  • 6