Skip to content
Advertisement

Detect all duplicates from second list based on the numbers in the first list

I have 2 lists, for example:

JavaScript

I want to get a list 3 which will include all numbers from list2 matching the numbers from list1:

JavaScript

I was trying to use 2 while loops to go through list1 and list2 and match numbers, but it gives me wrong number of values:

JavaScript

list3 = [1, 2, 2, 3, 3, 4, 4]

Is there any way to do it using loops?

  • Please do NOT provide solution using set() it doesn`t work for my lists!

Advertisement

Answer

With a for loop:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement