August 2020
Intermediate to advanced
508 pages
11h 53m
English
Suppose you are writing a JavaScript application, and somewhere in your code you find that you need to get the intersection between two arrays. The intersection is a list of all the values that occur in both of the arrays. For example, if you have the arrays, [3, 1, 4, 2] and [4, 5, 3, 6], the intersection would be a third array, [3, 4], since both of those values are common to the two arrays.
Here’s one possible implementation:
| | function intersection(firstArray, secondArray){ |
| | let result = []; |
| | |
| | for (let i = 0; i < firstArray.length; i++) { |
| | for (let j = 0; j < secondArray.length; j++) { |
| | if (firstArray[i] == secondArray[j]) { |
| | result.push(firstArray[i]); |
| | } |
| | } |
| | } |
| | return result; |
| | } |
Here, we are ...
Read now
Unlock full access