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

134
Views
Extracting common numbers from multi-dimensional array

I'm a little stuck on a JavaScript function that can extract common numbers from a nested array of numbers.

Example input:

[[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

Expected output:

[3, 10]; // because 3 and 10 are present in all 3 arrays

It must work with any arbitrary array of numbers of any size.

Ideas?

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You can do something like this:

var arr = [[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

var result = arr.shift().filter(function(v) {
    return arr.every(function(a) {
        return a.indexOf(v) !== -1;
    });
});

console.log(result);

about 4 years ago · Santiago Gelvez Report

0

It might pay to convert the inner arrays into ever shrinking Sets:

const arr = [[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

// Expected output:  [3, 10]; // because 3 and 10 are present in all 3 arrays


let final = new Set(arr[0])

for ( i=1; i < arr.length ; i++) {
  final = new Set (arr[i].filter ( x => final.has(x) ))
}

const newArr = Array.from(final)

console.log(newArr)

about 4 years ago · Santiago Gelvez Report

0

var arrays = [[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

var result = arrays.shift().reduce(function(res, v) {
    if (res.indexOf(v) === -1 && arrays.every(function(a) {
        return a.indexOf(v) !== -1;
    })) res.push(v);
    return res;
}, []);

document.write('<pre>' + JSON.stringify(result,null,4)+ '</pre>');
about 4 years ago · Santiago Gelvez 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!