0

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 
  }
}
Andy
  • 61,948
  • 13
  • 68
  • 95
Egor
  • 21
  • 1
  • 2
    Two different tasks. Why not two different loops? – Andy Aug 21 '22 at 16:50
  • for second task you can create a dictionary from A and check for B element in that dictionary , if key is there add to D array – Ankur Jyoti Phukan Aug 21 '22 at 16:56
  • Please fix the title of your question because this appears to have nothing at all to do with "sorting" which is the processing of reordering an array according to some criteria. You are comparing two arrays and creating two new arrays based on the comparison. – jfriend00 Aug 21 '22 at 17:16
  • 1
    A `Set` or a `Map` object is often very useful when looking for uniqueness because they provide unique keys and fast, efficient lookup for the existence of a key without your own looping. – jfriend00 Aug 21 '22 at 17:17

3 Answers3

1

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)

codetulpa
  • 36
  • 4
1

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
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)