How to find the shortest string in an array in JavaScript
Learning JavaScript is crucial for web development. JavaScript arrays are important for organizing data in dynamic web applications, playing a crucial role in client-side operations and server communication. Additionally, mastering basic algorithms, such as finding the shortest string in an array, is foundational, enhancing a programmer’s problem-solving skills for complex tasks in software development. JavaScript does provide a vast variety of building blocks for problem-solving, but there are no built-in functions to find the shortest element in the array. Let's see how to find the shortest element in the array using JavaScript.
Let’s consider the following array consisting of five strings:
0 | 1 | 2 | 3 | 4 |
"function" | "variable" | "array" | "algorithm" | "loop" |
The goal is to find the shortest string in this array, i.e., “loop.”
Finding the shortest string in an array
Given an array of strings, we can solve this problem as follows:
Select the string at the 0th index of the array as the shortest string
Starting from the 1st index of the array, iterate through the array and for each of the strings:
Compare the length of the string with the shortest string.
If the length of the current string is less than the shortest string, set this current string as the shortest string.
At the end of the loop, we will have the shortest string in the array.
Code example
const stringArray = ["function", "variable", "array", "algorithm", "loop"];let shortestString = stringArray[0];// Starting the loop from index 1 because index 0 is already stored in a variable for comparisonfor (let i = 1; i < stringArray.length; i++){const currentString = stringArray[i];if (currentString.length < shortestString.length) {shortestString = currentString;}}console.log("The shortest string is:", shortestString);
Code explanation
Line 1: We declare a JavaScript array.
Line 2: We initialize the variable
shortestStringwith the first element of the array.Line 4: The
forloop is created to iterate through the array, starting from the second element, i.e.,i = 1. (SinceshortestStringis initialized with string at index 0, starting the loop at index 1 will save an unnecessary comparison ofshortestStringwith string at index 0 of the array).Line 5: The string at the index
iis stored in thecurrentStringvariable.Line 7–9: The length of each string is being compared with the current
shortestString. If a shorter string is found,shortestStringis updated with thecurrentString.Line 12: The final result is printed to the console.
Here is a dry run of the example illustrating how the algorithm works:
|
|
|
|
|
1 | function | variable | False | No update |
2 | function | array | True | shortestString = array |
3 | array | algorithm | False | No update |
4 | array | loop | True | shortestString = loop |
Time and space complexity:
The time complexity of the provided code is
, where is the number of elements in the array. This is because the code iterates through the array once. The space complexity is
. Regardless of the size of the array, the amount of additional memory used by the algorithm remains constant, as the only additional variables used are shortestTerm,i, andcurrentTerm, and they do not depend on the size of the input array.
In conclusion, this simple algorithm allows us to find the shortest string in an array efficiently. This versatile approach can be adapted to similar problems where you need to find the minimum value based on a specific criterion.
Free Resources