2032. Two Out of Three LeetCode Solution
In this guide, you will get 2032. Two Out of Three LeetCode Solution with the best time and space complexity. The solution to Two Out of Three problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Two Out of Three solution in C++
- Two Out of Three solution in Java
- Two Out of Three solution in Python
- Additional Resources

Problem Statement of Two Out of Three
Given three integer arrays 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 <= 100
1 <= nums1[i], nums2[j], nums3[k] <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(100) = O(1)
2032. Two Out of Three LeetCode Solution in C++
class Solution {
public:
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2,
vector<int>& nums3) {
vector<int> ans;
vector<int> count(101);
for (const vector<int>& nums : {nums1, nums2, nums3})
update(count, nums);
for (int i = 1; i <= 100; ++i)
if (count[i] >= 2)
ans.push_back(i);
return ans;
}
private:
void update(vector<int>& count, const vector<int>& nums) {
for (const int num : unordered_set<int>(nums.begin(), nums.end()))
++count[num];
}
};
/* code provided by PROGIEZ */
2032. Two Out of Three LeetCode Solution in Java
class Solution {
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
List<Integer> ans = new ArrayList<>();
int[] count = new int[101];
for (int[] nums : new int[][] {nums1, nums2, nums3})
update(count, nums);
for (int i = 1; i <= 100; ++i)
if (count[i] >= 2)
ans.add(i);
return ans;
}
private void update(int[] count, int[] nums) {
for (final int num : Arrays.stream(nums).boxed().collect(Collectors.toSet()))
++count[num];
}
}
// code provided by PROGIEZ
2032. Two Out of Three LeetCode Solution in Python
class Solution:
def twoOutOfThree(
self,
nums1: list[int],
nums2: list[int],
nums3: list[int],
) -> list[int]:
count = collections.Counter()
for nums in nums1, nums2, nums3:
count.update(set(nums))
return [i for i, c in count.items() if c >= 2]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.