Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
how to get the array element which has a maximum length in the given array

I need to get the array element which has maximum length in the given array.

Suppose there is an array:

const a = ["apple","banana","mango","watermelon","grapes"];

Here I need to get watermelon as output because watermelon has the maximum length.

Below is what has been 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 an optimal solution to the problem.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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;
}
about 4 years ago · Juan Pablo Isaza Report

0

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 to acc
  • Else, simply return acc
about 4 years ago · Juan Pablo Isaza Report

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!