Watch 10 video solutions for Two Out of Three, a easy level problem involving Array, Hash Table, Bit Manipulation. This walkthrough by Competitive Somya has 1,530 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
nums1, nums2, and nums3, return a distinct array containing all the values that are present in at least two out of the three arrays. You may return the values in any order.
Example 1:
Input: nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3] Output: [3,2] Explanation: 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] Explanation: 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: [] Explanation: No value is present in at least two arrays.
Constraints:
1 <= nums1.length, nums2.length, nums3.length <= 1001 <= nums1[i], nums2[j], nums3[k] <= 100Problem Overview: You receive three integer arrays nums1, nums2, and nums3. The task is to return all distinct values that appear in at least two of the arrays. Duplicates inside the same array should not inflate the count; only presence across different arrays matters.
Approach 1: Using Sets to Store Unique Elements and Count Occurrences (Time: O(n + m + k), Space: O(n + m + k))
The most common solution uses set structures to remove duplicates inside each array first. Convert each array into a set, then track how many arrays contain each number using a hash map. Iterate through each set and increment a counter for its elements. Any value with a count of at least two appears in two or more arrays and belongs in the result.
This approach works well because set lookups and insertions are O(1) on average. By eliminating duplicates early, you avoid incorrectly counting repeated numbers from the same array. The technique relies heavily on hash table behavior and is the most readable solution for interviews.
Approach 2: Using Frequency Arrays (Time: O(n + m + k), Space: O(U))
If the value range is reasonably bounded, a frequency array can replace the hash map. First convert each input array into a set so duplicates inside one array are ignored. Then maintain a frequency array where the index represents a value and the stored number represents how many arrays contain it. Traverse the three sets and increment the frequency for each value.
After processing all sets, iterate through the frequency structure and collect numbers with frequency ≥ 2. This avoids hashing overhead and can be slightly faster in languages where array access is cheaper than map operations. The tradeoff is extra memory proportional to the maximum possible value, which may not be ideal for large ranges. This approach still relies on concepts from array processing and counting techniques often seen in bit manipulation or frequency problems.
Recommended for interviews: The set + hash map method is what most interviewers expect. It demonstrates that you handle duplicates correctly and leverage constant‑time hash lookups. Mentioning the frequency-array optimization shows deeper understanding of tradeoffs when the input range is known. Both run in linear time, but the hash-based approach is safer when constraints are large or unknown.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sets + Hash Map Counting | O(n + m + k) | O(n + m + k) | General case. Works for any value range and is the most common interview solution. |
| Frequency Array Counting | O(n + m + k) | O(U) | Useful when value range is small and predictable, making array indexing faster than hashing. |