S
S
stevieg82019-02-28 10:09:18
Arrays
stevieg8, 2019-02-28 10:09:18

How to check the number of occurrences of one array in another?

Hello. Please help me to resolve the issue. I am a beginner, I just started learning JS, I googled, but I did not find how to solve just such a question.
There are two arrays

arr1 = [1,2,3,4,5,6,7,8,9]
arr2 = [3,4,5]

How to display the number of occurrences of elements of the second array in the first?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lofem, 2019-02-28
@Lofem

Loop through the first array, and if the current character is equal to the first character from the second array, then start the comparison loop.
Exclusively for JS, there is a bad solution:
arr1.join().match(new RegExp(arr2.join(), "g")).length
Here each array is converted to a string and the number of occurrences is searched.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question