August 2024
Intermediate to advanced
516 pages
11h 47m
English
These are the solutions to the exercises found in the section Exercises.
The following implementation first stores the values of the first array in a hash table and then checks each value of the second array against that hash table:
| | function getIntersection(array1, array2) { |
| | const intersection = []; |
| | const hashTable = {}; |
| | |
| | array1.forEach((value) => { |
| | hashTable[value] = true; |
| | }); |
| | |
| | array2.forEach((value) => { |
| | if (hashTable[value]) { |
| | intersection.push(value); |
| | } |
| | }); |
| | |
| | return intersection; |
| | } |
This algorithm has an efficiency of O(N).
The following implementation checks each string in the array. If the string isn’t yet in the hash table, the string gets added. If the string is in the hash table, that ...
Read now
Unlock full access