Two Out of Three - Problem

Imagine you're analyzing voting patterns across three different regions. Given three integer arrays nums1, nums2, and nums3, you need to find all the popular values that appear in at least two out of the three arrays.

Your task is to return a distinct array containing all values that have this "majority support" - appearing in 2 or more of the input arrays. The order of values in your result doesn't matter, but each value should appear only once.

Example: If nums1 = [1,1,3,2], nums2 = [2,3], and nums3 = [3], then value 2 appears in arrays 1 and 2, while 3 appears in all three arrays. So the answer is [2,3].

Input & Output

example_1.py โ€” Basic Case
$ Input: nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
โ€บ Output: [2,3]
๐Ÿ’ก Note: Value 2 appears in nums1 and nums2. Value 3 appears in all three arrays. Value 1 only appears in nums1, so it's not included.
example_2.py โ€” No Common Elements
$ Input: nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
โ€บ Output: [1,2,3]
๐Ÿ’ก Note: Value 1 appears in nums1 and nums3. Value 2 appears in nums2 and nums3. Value 3 appears in nums1 and nums2. All values appear in at least two arrays.
example_3.py โ€” Single Elements
$ Input: nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
โ€บ Output: []
๐Ÿ’ก Note: No value appears in more than one array, so the result is empty. Each array contains completely different elements.

Visualization

Tap to expand
๐Ÿฝ๏ธ Restaurant Menu Analysis๐Ÿช Restaurant 1Menu: [Pizza, Burger, Pasta]Flag: ๐Ÿช (bit 1)๐Ÿฌ Restaurant 2Menu: [Burger, Pasta]Flag: ๐Ÿฌ (bit 2)๐Ÿซ Restaurant 3Menu: [Pasta]Flag: ๐Ÿซ (bit 4)๐ŸŽฏ Dish Popularity Tracker๐Ÿ• Pizza: ๐Ÿช๐Ÿ” Burger: ๐Ÿช๐Ÿฌ๐Ÿ Pasta: ๐Ÿช๐Ÿฌ๐ŸซโŒ Pizza: Only 1 restaurantโœ… Burger: 2 restaurantsโœ… Pasta: 3 restaurants๐Ÿ† Popular: [Burger, Pasta]
Understanding the Visualization
1
Visit Restaurant 1
Mark each unique dish with flag ๐Ÿช (bit 1)
2
Visit Restaurant 2
Mark each unique dish with flag ๐Ÿฌ (bit 2)
3
Visit Restaurant 3
Mark each unique dish with flag ๐Ÿซ (bit 4)
4
Find Popular Dishes
Dishes with 2+ flags are served by multiple restaurants
Key Takeaway
๐ŸŽฏ Key Insight: Use bit flags to efficiently track which arrays contain each element, then count bits to find elements in multiple arrays!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all three arrays to build the hash map

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for hash map storing unique elements, where k is number of distinct elements

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums1.length, nums2.length, nums3.length โ‰ค 100
  • 1 โ‰ค nums1[i], nums2[j], nums3[k] โ‰ค 100
  • All values are positive integers
  • Arrays can contain duplicate elements
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
78.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen