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

204
Views
El método de reducción de Javascript devuelve un valor incorrecto

Necesito encontrar la longitud de la subcadena más larga en una cadena, lo hago así:

 var lengthOfLongestSubstring = function(s) { let arr = []; let obj = {}; for(let i = 0; i<s.length; i++){ if(arr.indexOf(s[i])!==-1){ copy=arr.slice(); obj[i]=copy; arr=[]; arr.push(s[i]) } else{ arr.push(s[i]); } } console.log(obj) return Object.values(obj).reduce((acc, nextItem)=>{ if(acc&acc.length>=nextItem&&nextItem.length){ return acc.length; } else{ return nextItem.length } }, []) }; console.log (lengthOfLongestSubstring ('abcabcbb'));

Para el caso de prueba "abcabcbb" devuelve 1 en lugar de 3 y no entiendo por qué; por cierto, ¿qué complejidad (en términos de gran O) lograría mi código?

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

0

la variante que tiene debería funcionar, solo corrija algunos errores tipográficos en su código:

 return Object.values(obj).reduce((acc, nextItem) => acc >= nextItem.length ? acc.length : nextItem.length , 0)
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!