I have a nested Loop to compare two array indexes and log the equals indexes and non equal indexes but it make the indexes repeated

let bobsFollowers=['Ahmed','Mahmuod','ali','hussam'];

let tinasFollowers  = ['Ahmed','Ali','hussam'];

let mutualFollowers = [ ];

for (let i =0 ; i < bobsFollowers.length;i++){

for (let j = 0 ; j< tinasFollowers.length; j++){

 if (bobsFollowers[i] === tinasFollowers[j]){

   console.log('There is mutual Friends :'

   +tinasFollowers[j]

   );

   mutualFollowers.push(tinasFollowers[j]);

 }

 else if (bobsFollowers[i] !== tinasFollowers[j]){

   console.log('the Non Mutual Freinds are  ' +bobsFollowers[i]+' '+ tinasFollowers[j]);

 }

 else{

   console.log('There are All Mutual')

 }

}

}

console.log(mutualFollowers)

If you only want mutual followers which are the same position/index in both arrays, you only need a single loop.

With the nested loop, you get all mutual followers. You are comparing every element from the first array with every element from the second array

yes and need the unmutual followers , what the logic behind that

that is a good question, what are you thoughts on this?

You would need a different flow. For each follower of Bob, you would need to loop through all of Tina’s followers and keep track of Bob follower is mutual/shared with Tina. If after looping through all of Tina’s followers and the follower isn’t mutual, you have an un-mutual follower

1 Like

I tried to make the logic to collect the unmutual followers by else if statement ,but the output make that Un Compared value pushed in the un-mutual array . "Ahmed From bob’s is compared three times take the first Mutual and push the twice Compared to the Un-Mutual Array " … i appreciate that very much

that won’t work.

which is why I said you need a different flow. After having looped through Tina’s followers, and no matches found, you have determined you have an un-mutual follower, so you get something like:

for (let j = 0 ; j< tinasFollowers.length; j++){
   // do comparison, see if there is a mutual follower
}
// after Tina loop
// if no mutual followers where found among Tina's, now we can append to the un-mutual array