Here, we will quickly look at how you can get the longest string in a string array. Quickly and easily.
You can expect to get this question on interviews or sites like Codewars to test your programming skills.
First, let’s get an array!
Firstly we will use the .split method with an empty string to “split” each word into an array
const strArray = "When I wrote this code only God and I understood what I did Now only God knows".split(
" "
)
And we will get:
strArray = [
'When', 'I', 'wrote', 'this',
'code', 'only',
'God', 'and',
'I', 'understood',
'what', 'I',
'did', 'Now',
'only', 'God',
'knows'
]
1. For Loop
Firstly we will create a function with two variables:
- longestWord – We will save the current longest string in the array here
- charCount – We will save the length of the longest word here
Alternatively, you can just keep track of an index
function getLongestString () {
let longestWord = ""
let charCount = 0
}
then we will create the for loop
function getLongestString (array) {
let longestWord = ""
let charCount = 0
for (let i = 0; i < array.length; i++) {
}
}
next step will be comparing the length of the current string in a loop against charCount and if it is
longer than charCount swap longestWord for the current word and swap charCount as well
function getLongestString (array) {
let longestWord = ""
let charCount = 0
for (let i = 0; i < array.length; i++) {
if (array[i].length > charCount) {
longestWord = array[i]
charCount = array[i].length
}
}
}
the last step is to just simply to return the longestWord
function getLongestString (array) {
let longestWord = ""
let charCount = 0
for (let i = 0; i < array.length; i++) {
if (array[i].length > charCount) {
longestWord = array[i]
charCount = array[i].length
}
}
return longestWord
}
2. Sort
Firstly we will create a function that will return the first element of what will .sort function returns.
function getLongestString (array) {
return array.sort(
)[0]
}
The next and also last step is to write compare function inside the sort function.
function getLongestString (array) {
return array.sort(
function (string1, string2) {
return string2.length - string1.length
}
)[0]
}
And the return of this function will be the longest string
3. Reduce
And my personal favorite, we will use reduce function.
We will start by creating a function that will return the return of reduce function,
might be a bit confusing, apologies
function getLongestString (array) {
return array.reduce(
)
}
Again, the next and last step is to write a function that will take two strings and
compare them, and will return the longest one. So you will end up with just one (longest) string.
function getLongestString (array) {
return array.reduce(
function (str1, str2) {
return str1.length > str2.length ? str1 : str2;
}
)
}
And reduce method is done, Job well done.
And there you go! 3 ways to get the longest string in an array in Javascript. If you actually looked at all three and
wondering with which should you go, personally I would go with reduce function.