Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda