Two Out of Three — Solution in Python, Java & C++
Two Out of Three solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays and hash table.
Two Out of Three is an easy coding problem on arrays, hash table and bit manipulation, asked in Amazon and Google interviews.
- Difficulty: Easy
- Topics: Arrays, Hash Table, Bit Manipulation
- Asked at: Amazon, Google
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given three integer arrays, return every distinct value that appears in at least two of them.
Return the values in ascending order; if there are none, return an empty array.
Example 1
Input: nums1 = [4,7,7,1], nums2 = [7,2], nums3 = [1,9]
Output: [1,7]
Explanation: 1 is in nums1 and nums3; 7 is in nums1 and nums2.
Example 2
Input: nums1 = [5], nums2 = [6], nums3 = [8]
Output: []
Example 3
Input: nums1 = [3,2], nums2 = [2,3], nums3 = [3,2]
Output: [2,3]
Constraints
1 <= nums1.length, nums2.length, nums3.length <= 1001 <= nums1[i], nums2[j], nums3[k] <= 100
Hints for Two Out of Three
Hint 1
Repeats inside one array do not count twice — only which arrays contain a value matters.
Hint 2
Give each value a 3-bit mask: bit
kset when the value occurs in arrayk.Hint 3
Values are at most 100: scan
vfrom 1 to 100 and keep those whose mask has at least two bits set.
How to solve Two Out of Three
Record, for every value, the set of arrays it appears in as a 3-bit mask; a value qualifies when its mask has two or more bits.
Approach
- Create
mask[0..100]filled with 0. - For array
k(0, 1, 2) and each of its valuesv, setmask[v] |= 1 << k. - Scan
vfrom 1 to 100 and collect it ifmask[v]is 3, 5, 6 or 7 (at least two bits).
Why it works
OR-ing a bit is idempotent, so duplicates within one array do not inflate the count; the mask records exactly which arrays contain v. Scanning values in increasing order produces the required ascending output with no sort.
Complexity
- Time —
O(n1 + n2 + n3 + V) with V = 100 - Space —
O(V)
Pitfalls
- Counting occurrences instead of arrays wrongly accepts
[7,7]appearing in only one array. - Each value must be reported once, even if it is in all three arrays.
- The output order must be ascending here.
Reference solution
Python
from typing import List
def twoOutOfThree(nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
s1, s2, s3 = set(nums1), set(nums2), set(nums3)
return sorted(v for v in s1 | s2 | s3 if (v in s1) + (v in s2) + (v in s3) >= 2)Java
public static int[] twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
int[] mask = new int[101];
for (int v : nums1) mask[v] |= 1;
for (int v : nums2) mask[v] |= 2;
for (int v : nums3) mask[v] |= 4;
List<Integer> out = new ArrayList<>();
for (int v = 1; v <= 100; v++) {
if (Integer.bitCount(mask[v]) >= 2) out.add(v);
}
int[] res = new int[out.size()];
for (int i = 0; i < res.length; i++) res[i] = out.get(i);
return res;
}C++
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
vector<int> mask(101, 0);
for (int v : nums1) mask[v] |= 1;
for (int v : nums2) mask[v] |= 2;
for (int v : nums3) mask[v] |= 4;
vector<int> out;
for (int v = 1; v <= 100; v++) {
int m = mask[v];
if (m == 3 || m == 5 || m == 6 || m == 7) out.push_back(v);
}
return out;
}JavaScript
var twoOutOfThree = function(nums1, nums2, nums3) {
var mask = new Array(101).fill(0);
var arrays = [nums1, nums2, nums3];
for (var k = 0; k < 3; k++) {
for (var i = 0; i < arrays[k].length; i++) mask[arrays[k][i]] |= 1 << k;
}
var out = [];
for (var v = 1; v <= 100; v++) {
var m = mask[v];
if (m === 3 || m === 5 || m === 6 || m === 7) out.push(v);
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Hashing: Hash Maps and Hash Sets