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

145
Views
Verifique si hay un producto duplicado entre 3 matrices usando hash

Tengo una lista de productos cuyos datos se han dividido en 3 matrices que contienen el nombre, el precio y el peso del producto. ¿Cómo hago una función que encuentre el producto duplicado usando el mapa hash?

 //Inputs: name = ["ball", "bat", "glove", "glove", "glove"] price = [2, 3, 1, 2, 1] weight = [2, 5, 1, 1, 1] //Output: true //Inputs: name = ["ball", "bat", "glove", "glove", "glove"] price = [2, 3, 1, 2, 2] weight = [2, 5, 1, 1, 2] //Output: false

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

0

Javascript no tiene un HashSet , pero hay un Set que almacenará todo lo que quieras. Entonces, para probar si hay duplicados:

  • Crear un Set vacío

  • Iterar sobre i = 0..items.length

  • Para cada i , cree un hash a partir de name[i] , price[i] y weight[i] . Nota: no importa cómo lo haga, siempre que lo siguiente sea cierto (observe el 3x === ):

    hash(name[i], price[i], weight[i]) === hash(name[i], price[i], weight[i])

  • Después de crear un hash, verifique si ya existe en su Set

    • Si existe, hay un duplicado y puede regresar instantáneamente
    • Si no es así, agrega el hash a su conjunto y continúa el ciclo
  • Llegar al final del bucle sin un retorno anticipado significa que no hay duplicados

 let name, price, weight; const makeHash = (name, price, weight) => `${name}__${price}__${weight}`; const duplicateTest = () => { const seen = new Set(); for (let i = 0; i < name.length; i += 1) { const hash = makeHash(name[i], price[i], weight[i]); if (seen.has(hash)) return true; seen.add(hash); } return false; } name = ["ball", "bat", "glove", "glove", "glove"] price = [2, 3, 1, 2, 1] weight = [2, 5, 1, 1, 1] console.log(duplicateTest()); // Output: true name = ["ball", "bat", "glove", "glove", "glove"] price = [2, 3, 1, 2, 2] weight = [2, 5, 1, 1, 2] console.log(duplicateTest()); // Output: false

about 4 years ago · Juan Pablo Isaza Report

0

En primer lugar, desafiaría la necesidad de dividir los datos en tres matrices y, en última instancia, abordaría el problema de esta manera si la estructura de datos no pudiera modificarse.

 const findDupes = ( [name, ...names], [price, ...prices], [weight, ...weights], res = {}, ) => { const next = ($res) => findDupes(names, prices, weights, $res); const hash = `${name}\/${price}\/${weight}`; const dupe = res[hash] ? { name, price, weight } : []; return [].concat(dupe).concat( names.length ? next({ ...res, [hash]: true }) : [], ); }; const name = ["ball", "bat", "glove", "glove", "glove"]; const price = [2, 3, 1, 2, 1]; const weight = [2, 5, 1, 1, 1]; console.log( findDupes(name, price, weight), );

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!