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

154
Views
Check if there is a duplicate product between 3 arrays using hash

I have a list pf products whose data has been split into 3 arrays that holds the name, price, and weight of the product. How do I make a function that finds the duplicate product using hash map?

//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 doesn't have a HashSet, but there is a Set that'll store anything you like. So, to test for duplicates:

  • Create an empty Set

  • Iterate over i = 0..items.length

  • For every i, create a hash out of name[i], price[i] and weight[i]. Note: it doesn't matter how you do it, as long as the following is true (notice the 3x ===):

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

  • After creating a hash, check if it already exists in your Set

    • If it exists, there's a duplicate and you can instantly return
    • If it doesn't, you add the hash to your set and continue the loop
  • Reaching the end of the loop without an early return means there are no duplicates

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

I would firstly challenge the need to split the data into three arrays, then ultimately approach the problem this way if the data structure couldn't be reshaped.

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!