I need to get the array element which has maximum length in the given array.
Suppose a=["apple","banana","mango","watermelon","grapes"]
Here I need to get the watermelon as output because watermelon has the maximum length.
Please give the solution in java script
Actually below is the code I have tried
let length7=0
let maxlength=function fn(){
for(i7=0;i7<a.length;i7 ){
if(a[i7].length>length7){
length7=a[i7].length
}
}
return length7 }
console.log(maxlength())
From this I am getting the length of the maximum string(as 10). But I need to get the output as watermelon.
Please explain the ways to solve the problem...
CodePudding user response:
You could store the longest element in a seperate variable and return that.
let maxlength = function fn() {
let length7 = 0;
let value = "";
for (i7 = 0; i7 < a.length; i7 ) {
if (a[i7].length > length7) {
length7 = a[i7].length;
value = a[i7];
}
}
return value;
}
CodePudding user response:
The below solution may be one possible solution to achieve the desired objective.
Code Snippet
const getLongestWordIn = arr => (
arr.reduce(
(acc, word) => (word.length > acc.length ? word : acc),
""
)
);
const a = ["apple","banana","mango","watermelon","grapes"];
console.log(getLongestWordIn(a));
Explanation
- Use
.reduce
to iterate over the array - The aggregator/accumulator named
acc
is initialized as empty string""
- If the word (being iterated) has length more than
acc
, then assign that word toacc
- Else, simply return
acc
CodePudding user response:
You need a way to save the index of the maximum length of the array. Easiest way is to save the index (i7) in a global variable. Then at the end of the iterations instead of saving the maximum length you will have the index of the item with the maximum lenght. Then you just access it from the array. An examen of the code:
let maxLengthIndex=0
let maxlength=function fn(){
for(i7=0 ; i7<a.length ; i7 ){
if(a[i7].length > a[maxLengthIndex].length){
maxLengthIndex = i7
}
}
return a[maxLengthIndex]
}
CodePudding user response:
You can store the longest string on each iteration and replace it by the next element in the array if it's longer.
const array = ["apple", "banana", "mango", "watermelon", "grapes"];
let longestStr = '';
array.forEach((str) => {
if (str.length > longestStr.length) {
longestStr = str;
}
});
console.log(longestStr);
You can also use reduce function on arrays to return the longest. Documentation for array.reduce()
const longestStr = array.reduce((prev, current) => current.length > prev.length ? current : prev, '');
CodePudding user response:
You need to store either (1) just the element with the maximum length or (2) both the element and it's maximum length. You should also move the current max
variable (max7
in your case) inside the function. Putting this together and opting for (2):
function longestElement(arr) {
if (!arr.length) return; // empty array
let longest = arr[0];
for (let i = 1; i < arr.length; i )
longest = arr[i].length > longest.length ? arr[i] : longest;
return longest;
}
let a=["apple","banana","mango","watermelon","grapes"];
console.log(longestElement(a)); // "watermelon"
If you want to shorten this, you can use reduce
:
const longestElement = (arr) => arr.reduce((x, y) => x.length > y.length ? x : y);