Created
April 17, 2020 18:13
-
-
Save Nicknyr/265f49f7c59a0054089aaea5c9530538 to your computer and use it in GitHub Desktop.
CodeSignal - All Longest Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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(inputArray) { | |
let arr = inputArray; | |
let longest = 0; | |
let longestArr = []; | |
// Iterate through array to find longest length string(s) | |
for(let i = 0; i < arr.length; i++) { | |
if(arr[i].length >= longest) { | |
longest = arr[i].length; | |
} | |
} | |
// Compare each element in arr to see if it's equal to longest | |
// Push the longest words into longestArr array | |
for(let j = 0; j < arr.length; j++) { | |
if(arr[j].length === longest) { | |
longestArr.push(arr[j]); | |
} | |
} | |
return longestArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment