Skip to content

Instantly share code, notes, and snippets.

@Keijiro-Ida
Created May 19, 2025 22:24
Show Gist options
  • Save Keijiro-Ida/8e81347cace4b4f7f8bcddcf6235a07e to your computer and use it in GitHub Desktop.
Save Keijiro-Ida/8e81347cace4b4f7f8bcddcf6235a07e to your computer and use it in GitHub Desktop.
349. Intersection of Two Arrays
function intersection(nums1: number[], nums2: number[]): number[] {
const nums1Set = new Set(nums1);
const nums2Set = new Set(nums2);
const result: number[] = [];
for(const num of nums1Set) {
if(nums2Set.has(num)) {
result.push(num);
}
}
return result;
};
class Solution {
/**
* @param Integer[] $nums1
* @param Integer[] $nums2
* @return Integer[]
*/
function intersection($nums1, $nums2) {
$nums1_unique = array_unique($nums1);
$nums2_map = array_flip(array_unique($nums2));
$result = [];
foreach($nums1_unique as $num) {
if(isset($nums2_map[$num])) {
$result[] = $num;
}
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment