how to find the longest string in an array java code example
Example 1: Convert longest string in array
const findLongest = words => Math.max(...(words.map(el => el.length)));
findLongest(['always','look','on','the','bright','side','of','life']);
Example 2: find shortest string in array java
public static String smallest(String words[]) {
if (words == null || words.length < 1) {
return "";
}
String smallest = words[0];
for (int i = 1; i < words.length; i++) {
if (words[i].length() < smallest.length()) {
smallest = words[i];
}
}
return smallest;
}