Finding all the Longest Strings from an Array in JavaScript


In the given problem statement we have to find all the longest strings from an array with the help of Javascript functionalities. So basically this task can be done by getting the length of each string and then comparing these lengths with the maximum length.

Understanding the Problem

The problem at hand is to find the longest strings from an array in Javascript. So we will have an array of strings and our main task is to identify the strings which have the maximum length and show them as a new array. For example: suppose we have an array of strings ['abc', 'defg', 'hijkl', 'mnopqr', 'stuvwxyz'] So the longest string in this array is [ 'stuvwxyz' ]. So we have to implement the function to do the same task.

Logic for the given Problem

To solve this problem we will define a function to do this task. And inside the function first we have to find the length of the strings. So for this we will iterate the array of strings and keep track of the maximum length found. For every string in the array we will compare the length with the current maximum length and update it if required. So we will have the length of the longest string.

Now we will filter the longest string by iterating over the array of strings again and this time we will use the filter method. If the length matches with the maximum length found. So we will include it in the array. And finally we will return the new array which will contain all the longest strings.

Algorithm

Step 1: As we have to find the longest string in the given array. So for doing this task we will create a function called findLongestStrings and this function will take an array as input. Inside this array we will have strings. And we will find the longest strings from them.

Step 2: Now we will use a variable to store the value of the maximum length of the strings. And initialize it with the value zero.

Step 3: In this step we need to find the lengths of the strings present in the array and find the maxLength of strings. So we will iterate through the array items.

Step 4: Inside the loop we will check the condition that the length of the string is greater than the maxLength if this condition is true then update the maxLength value with the length of the current string. Follow this step until we have not found the maxLength.

Step 5: So we will filter the array of strings with the longest string. For doing this task we will iterate over the array of strings with the help of a filter method. For every string we will check if its length matches with the maxLength. If the lengths are equal then we will return the new array which will have the longest strings in the array.

Example

  // Function to find the longest string
function longestStrings(array) {
  let maxLength = 0;
  for (let i = 0; i < array.length; i++) {
   if (array[i].length > maxLength) {
     maxLength = array[i].length;
   }
  }
  const longestStrs = array.filter((str) => str.length === maxLength);

  return longestStrs;
}
const strings = ["strawberry", "banana", "kiwi", "orange", "pear"];
const longest = longestStrings(strings);
console.log(longest);

Output

[ 'strawberry' ]

Complexity

The time complexity for finding the longest string from an array is O(n), in which n is the number of strings present in the array. As we have performed two basic operations. First is to find the maximum length and second is to match the length of the string with the maximum length calculated. The space complexity of this function is O(1) to O(n), because it depends upon the number of longest strings found in the array.

Conclusion

The code we have implemented solves the problem effectively. This code efficiently finds the maxi length and longest string in the array. The complexity of the code is linear which is making the code efficient for any size of arrays.

Updated on: 14-Aug-2023

407 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements