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

234
Views
How to find the common elements in 2 arrays in Javascript with no duplicates (unless there are 2 of the same element)?

For example, if I have 2 arrays being

['dog', 'cat', 'zebra', 'lion', 'cat']

and

['dog', 'cat', 'cat', 'frog']

then a new array should be created that only contains

['dog', 'cat', 'cat']
almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use two loop for the same and compare them from each other. Ex.

for(let i=0; i<ar1.length; i++)
 {
     for(let j=0; j<ar2.length; j++){
       if(ar1[i]==ar[2])
           res.push(ar[i]);
almost 4 years ago · Santiago Trujillo Report

0

var array1 = ['dog', 'cat', 'zebra', 'lion', 'cat'];
var array2 = ['dog', 'cat', 'cat', 'frog'];

// Then you can use filter

// const filteredArray = array1.filter(value => array2.includes(value));


// (OR) `indexOf` for old browsers compability

var filteredArray = array1.filter(function(n) {
  return array2.indexOf(n) !== -1;
});

console.log(filteredArray);

almost 4 years ago · Santiago Trujillo Report

0

const a = ['dog', 'cat', 'zebra', 'lion', 'cat'];
const b = ['dog', 'cat', 'cat', 'frog'];

let result = a.filter(function(obj){
    return b.indexOf(obj) !== -1
})
  
console.log(result)

almost 4 years ago · Santiago Trujillo 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!