Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. 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 is to find all the longest strings from an array in JavaScript. We have an array of strings and our task is to identify the strings with maximum length and return them as a new array. For example: if we have an array ['abc', 'defg', 'hijkl', 'mnopqr', 'stuvwxyz'], the longest string is 'stuvwxyz' with 8 characters.
Algorithm
Step 1: Create a function called longestStrings that takes an array of strings as input.
Step 2: Initialize a variable maxLength to store the maximum length found, starting with 0.
Step 3: Iterate through the array to find the maximum length among all strings.
Step 4: Compare each string's length with maxLength and update it if a longer string is found.
Step 5: Filter the array to return only strings that match the maximum length.
Example
// Function to find all the longest strings
function longestStrings(array) {
let maxLength = 0;
// Find the maximum length
for (let i = 0; i < array.length; i++) {
if (array[i].length > maxLength) {
maxLength = array[i].length;
}
}
// Filter strings with maximum length
const longestStrs = array.filter((str) => str.length === maxLength);
return longestStrs;
}
const strings = ["strawberry", "banana", "kiwi", "orange", "pear"];
const longest = longestStrings(strings);
console.log("Input array:", strings);
console.log("Longest strings:", longest);
Input array: [ 'strawberry', 'banana', 'kiwi', 'orange', 'pear' ] Longest strings: [ 'strawberry' ]
Example with Multiple Longest Strings
function longestStrings(array) {
let maxLength = 0;
for (let i = 0; i < array.length; i++) {
if (array[i].length > maxLength) {
maxLength = array[i].length;
}
}
return array.filter((str) => str.length === maxLength);
}
const strings2 = ["hello", "world", "javascript", "programming"];
const longest2 = longestStrings(strings2);
console.log("Input array:", strings2);
console.log("Longest strings:", longest2);
Input array: [ 'hello', 'world', 'javascript', 'programming' ] Longest strings: [ 'javascript', 'programming' ]
Alternative Approach Using Math.max()
function longestStringsAlt(array) {
// Find max length using Math.max and map
const maxLength = Math.max(...array.map(str => str.length));
// Return all strings with max length
return array.filter(str => str.length === maxLength);
}
const strings3 = ["cat", "dog", "elephant", "butterfly"];
const longest3 = longestStringsAlt(strings3);
console.log("Input array:", strings3);
console.log("Longest strings:", longest3);
Input array: [ 'cat', 'dog', 'elephant', 'butterfly' ] Longest strings: [ 'butterfly' ]
Complexity Analysis
The time complexity for finding the longest strings from an array is O(n), where n is the number of strings in the array. We perform two operations: finding the maximum length and filtering strings that match this length. The space complexity is O(k), where k is the number of longest strings found in the array.
Conclusion
This approach efficiently finds all strings with maximum length in an array using a two-pass algorithm. The linear time complexity makes it suitable for arrays of any reasonable size.
