Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Identifying the identical elements in two arrays with same location(index)

Ok, so I want to return the elements that are present in both provided arrays having the same index location.

exe1 = [A,B,C,D,N]
exe2 = [B,D,C,A,T]

it should return only C

I’ve tried looping them by nested loops but doesn’t work, here is what I’ve tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 let testing = []
       for (let i = 0; i < exe1.length; i++){
         for(let j = 0; j < exe2.length; j++){
           if(exe1[i] === exe2[j]){
             testing.push(exe1[i])
           }
         }
       };
       return testing;

mind the names of the arrays, please

>Solution :

You can use a simple filter to only include values in exe1 that have the same value at the same index of exe2.

const exe1 = ['A','B','C','D','N'];
const exe2 = ['B','D','C','A','T'];

const testing = exe1.filter((val, i) => val === exe2[i]);

console.log(testing)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading