Leetcode: Intersection of two arrays II
Given two arrays,
nums2, return the intersection of those arrays in any order.
The easiest way I was able to come up with was to sort the arrays first and then look at the beginning of the arrays. If both are equal, add the element to the output and pop the first element from each array. Otherwise, pop the smaller element of both arrays. Repeat this until one of the arrays is empty at which point there can be no more intersecting elements.
Runtime: 62 ms, faster than 80.36% of Python3 online submissions for Intersection of Two Arrays II. Memory Usage: 14.2 MB, less than 14.77% of Python3 online submissions for Intersection of Two Arrays II.