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

257
Views
How sort arrays in js

There are arrays A and B. We need to add to array C all the values of arrays A and B that are equal in both value and indexes.

A = [a,b,c], B = [c,b,a], C = [b]

Also: to add to array D all unique values from array A that are contained in array B.

A = [d,a,b,c], B = [c,b,a,a], D = [a,b,c]

Is it possible to do this without a nested loop? I can handle the first task, but how do I fill D with values?

for (let i = 0; i < a.length; i++) {
  if (b[i] === a[i]) {
    c.push(a[i]);
  } else if() {
   // Some code 
  }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

filter method? (for second question)

let D = A.filter(e => B.includes(e));

adding because you asked for D to contain unique values from A, thanks to @jarmod for pointing this out. You could use filter again to remove duplicates. I found this: Get all unique values in a JavaScript array (remove duplicates)

about 4 years ago · Juan Pablo Isaza Report

0

You can use a Set to keep track of unique values and .has() for efficient lookup in that Set. This code uses only one loop in your code, but several set operations use loops in their internal implementation (to build a set from an Array or convert a set to an Array). That cannot be avoided.

const A = ['e', 'd', 'a', 'b', 'c'];
const B = ['e', 'c', 'b', 'a', 'a'];


function processArrays(a, b) {
    const c = [];
    const aSet = new Set(a);
    const commonSet = new Set();

    for (let i = 0; i < a.length; i++) {
        if (b[i] === a[i]) {
            c.push(a[i]);
        }
        // if aSet has this value, then add it to commonSet
        if (aSet.has(b[i])) {
            commonSet.add(b[i])
        }
    }
    return { samePositionElements: c, commonElements: Array.from(commonSet) };
}

console.log(processArrays(A, B));

Note, I don't do an if/else here because these are two independent output tests. The first is if the two elements in the same position have the same value. The second is whether it's an element in common with the other array (regardless of position).

This code assumes the two input arrays have the same length. If you wish to support inputs with different length, that can be accommodated with slightly more code.

about 4 years ago · Juan Pablo Isaza Report

0

Simple Way and Fast Solution:

let A = ['a','b','c', 'e'];
let B = ['c','b','a', 'e'];

let common =[];
for(let i=0; i<A.length && i<B.length; i++){
 if(A[i]==B[i]){
   common.push(A[i])
 }
}

console.log(common)

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!