code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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:47:16+05:30 · Edit