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
class Solution:
def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
s1, s2, s3 = set(nums1), set(nums2), set(nums3)
ans = []
for i in range(1, 101):
a, b, c = i in s1, i in s2, i in s3
if a + b + c > 1:
ans.append(i)
return ans
class Solution {
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
int[] s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
List<Integer> ans = new ArrayList<>();
for (int i = 1; i <= 100; ++i) {
if (s1[i] + s2[i] + s3[i] > 1) {
ans.add(i);
}
}
return ans;
}
private int[] get(int[] nums) {
int[] s = new int[101];
for (int num : nums) {
s[num] = 1;
}
return s;
}
}
class Solution {
public:
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
vector<int> ans;
for (int i = 1; i <= 100; ++i)
if (s1[i] + s2[i] + s3[i] > 1)
ans.push_back(i);
return ans;
}
vector<int> get(vector<int>& nums) {
vector<int> s(101);
for (int num : nums) s[num] = 1;
return s;
}
};
func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) []int {
s1, s2, s3 := get(nums1), get(nums2), get(nums3)
var ans []int
for i := 1; i <= 100; i++ {
a, b, c := 0, 0, 0
if s1[i] {
a++
}
if s2[i] {
b++
}
if s3[i] {
c++
}
if a+b+c > 1 {
ans = append(ans, i)
}
}
return ans
}
func get(nums []int) map[int]bool {
s := make(map[int]bool, 101)
for _, num := range nums {
s[num] = true
}
return s
}