Two Out of Three

Easy
1114.

Two Out of Three

Two Out of Three is an easy coding problem on arrays, hash table and bit manipulation, asked in Amazon and Google interviews.

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 <= 100
  • 1 <= nums1[i], nums2[j], nums3[k] <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @param {number[]} nums3
 * @return {number[]}
 */
var twoOutOfThree = function(nums1, nums2, nums3) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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.

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 <= 100
  • 1 <= nums1[i], nums2[j], nums3[k] <= 100

Hints for Two Out of Three

  1. Hint 1

    Repeats inside one array do not count twice — only which arrays contain a value matters.

  2. Hint 2

    Give each value a 3-bit mask: bit k set when the value occurs in array k.

  3. Hint 3

    Values are at most 100: scan v from 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

  1. Create mask[0..100] filled with 0.
  2. For array k (0, 1, 2) and each of its values v, set mask[v] |= 1 << k.
  3. Scan v from 1 to 100 and collect it if mask[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