filmov
tv
LeetCode: 350. Intersection Of Two Arrays Javascript Solution
Показать описание
My initial thought was to use a brute force approach with nested for loops to iterate over nums1 and nums2 respectively then push smaller number to a result array, this approach worked but it did not take into account that input arrays may not be sorted and the fact there could be duplicates, so it failed test cases because of that.
Instead I decided to use two different approaches depending on whether input arrays are sorted or not:
A hashmap to store all elements from nums1 as keys and all their occurrences count as values, check if an element from nums2 is present in hashmap, if yes push it to the result array.
A two pointer technique to iterate over both arrays (given they are sorted) and push element to result array when intersection is found.
Solution on Github:
Detailed thought process:
Medium Article:
Instead I decided to use two different approaches depending on whether input arrays are sorted or not:
A hashmap to store all elements from nums1 as keys and all their occurrences count as values, check if an element from nums2 is present in hashmap, if yes push it to the result array.
A two pointer technique to iterate over both arrays (given they are sorted) and push element to result array when intersection is found.
Solution on Github:
Detailed thought process:
Medium Article: