classSolution: deftwoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]: mask = defaultdict(int) for i, nums inenumerate((nums1, nums2, nums3)): for x in nums: mask[x] |= 1 << i return [x for x, m in mask.items() if m & (m - 1)]
int* twoOutOfThree(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums3, int nums3Size, int* returnSize) { int mp[MAX_NUM + 1]; memset(mp, 0, sizeof(mp)); for (int i = 0; i < nums1Size; i++) { mp[nums1[i]] = 1; } for (int i = 0; i < nums2Size; i++) { mp[nums2[i]] |= 2; } for (int i = 0; i < nums3Size; i++) { mp[nums3[i]] |= 4; } int *res = (int *)malloc(sizeof(int) * MAX_NUM); int pos = 0; for (int i = 1; i <= MAX_NUM; i++) { if (mp[i] & (mp[i] - 1)) { res[pos++] = i; } } *returnSize = pos; return res; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var twoOutOfThree = function(nums1, nums2, nums3) { const map = newMap(); for (const i of nums1) { map.set(i, 1); } for (const i of nums2) { map.set(i, (map.get(i) || 0) | 2); } for (const i of nums3) { map.set(i, (map.get(i) || 0) | 4); } const res = []; for (const [k, v] of map.entries()) { if ((v & (v - 1)) !== 0) { res.push(k); } } return res; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functwoOutOfThree(nums1, nums2, nums3 []int) (ans []int) { mask := map[int]int{} for i, nums := range [][]int{nums1, nums2, nums3} { for _, x := range nums { mask[x] |= 1 << i } } for x, m := range mask { if m&(m-1) > 0 { ans = append(ans, x) } } return }