LeetCode Problem Workspace
Two Out of Three
Identify all elements appearing in at least two of three arrays using efficient scanning and hash lookups for fast verification.
3
Topics
6
Code langs
3
Related
Practice Focus
Easy · Array scanning plus hash lookup
Answer-first summary
Identify all elements appearing in at least two of three arrays using efficient scanning and hash lookups for fast verification.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array scanning plus hash lookup
This problem requires quickly determining which numbers appear in at least two of the three input arrays. Use hash sets to track presence in each array and aggregate counts efficiently. The pattern focuses on array scanning plus hash lookup to avoid repeated full searches and minimize time complexity while handling up to 100 elements per array.
Problem Statement
Given three integer arrays nums1, nums2, and nums3, return a list of all integers that appear in at least two of the arrays. Each list may contain duplicates, but the result should contain unique numbers only. The order of the result does not matter.
For example, if nums1 = [1,1,3,2], nums2 = [2,3], and nums3 = [3], the output should be [3,2] because 3 appears in all three arrays and 2 appears in nums1 and nums2. Constraints: 1 <= nums1.length, nums2.length, nums3.length <= 100 and 1 <= nums1[i], nums2[j], nums3[k] <= 100.
Examples
Example 1
Input: nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
Output: [3,2]
The values that are present in at least two arrays are:
- 3, in all three arrays.
- 2, in nums1 and nums2.
Example 2
Input: nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
Output: [2,3,1]
The values that are present in at least two arrays are:
- 2, in nums2 and nums3.
- 3, in nums1 and nums2.
- 1, in nums1 and nums3.
Example 3
Input: nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
Output: []
No value is present in at least two arrays.
Constraints
- 1 <= nums1.length, nums2.length, nums3.length <= 100
- 1 <= nums1[i], nums2[j], nums3[k] <= 100
Solution Approach
Use hash sets to record each array
Convert each array into a set to remove duplicates and allow O(1) membership checks. This ensures we only count unique numbers per array when scanning for overlaps.
Count occurrences across arrays
Iterate through all unique numbers from nums1, nums2, and nums3. Use a dictionary to track in how many arrays each number appears. If the count reaches two or more, add it to the result list.
Return the aggregated result
After scanning all arrays, collect numbers that appeared in at least two arrays. This method guarantees correct output while keeping operations efficient and avoiding repeated full searches.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n1+n2+n3) because each array is scanned once and set operations are O(1). Space complexity is O(n1+n2+n3) to store unique numbers from each array in sets and a count map.
What Interviewers Usually Probe
- The candidate immediately considers using hash sets for quick membership checks.
- They attempt to avoid nested loops over arrays to reduce time complexity.
- They clearly separate counting elements per array from aggregating the final result.
Common Pitfalls or Variants
Common pitfalls
- Counting duplicates within the same array instead of across arrays.
- Using nested loops over arrays, causing unnecessary O(n^2) time.
- Forgetting to remove duplicates before counting, leading to incorrect results.
Follow-up variants
- Find numbers present in exactly two arrays instead of at least two.
- Arrays contain negative numbers, requiring careful set handling.
- Arrays are very large, emphasizing memory-efficient counting methods.
FAQ
What is the main strategy for Two Out of Three?
Use hash sets for each array and count how many arrays each number appears in to find numbers in at least two arrays.
Can duplicates in a single array affect the result?
No, duplicates within the same array are ignored; only presence across multiple arrays matters.
How does the array scanning plus hash lookup pattern help?
It allows O(1) checks for element presence and avoids repeated full array searches, keeping the solution efficient.
What if all arrays are empty?
The result will be an empty list because no number appears in at least two arrays.
Are the output numbers ordered?
No, the problem only requires the list of numbers; their order does not matter.
Solution
Solution 1: Array + Enumeration
We can first put each element of the arrays into an array, then enumerate each number $i$ from $1$ to $100$, and check whether $i$ appears in at least two arrays. If so, add $i$ to the answer array.
class Solution:
def twoOutOfThree(
self, nums1: List[int], nums2: List[int], nums3: List[int]
) -> List[int]:
s1, s2, s3 = set(nums1), set(nums2), set(nums3)
return [i for i in range(1, 101) if (i in s1) + (i in s2) + (i in s3) > 1]Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array scanning plus hash lookup
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward