code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

006 All Longest Strings

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

// Given an array of strings, return another array containing all of its longest strings.

// Example

// For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

function allLongestStrings(values) {
  let longestString = values[0].length
  values.forEach((str) => {
    if(str.length > longestString){
      longestString = str.length
    }
  })
  return values.filter((str) => str.length === longestString)
}

test('all Longest Strings', () => {
  expect(allLongestStrings(["aba", "aa", "ad", "vcd", "aba"])).toEqual(["aba", "vcd", "aba"])
});

Created 2019-12-05T02:35:16+05:18 · Edit