Given three integer arrays nums1, nums2, and nums3, return adistinct 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.
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.
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.
import java.util.*;
classSolution {
public List<Integer>twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
Set<Integer> s1 =new HashSet<>(), s2 =new HashSet<>(), s3 =new HashSet<>();
for (int x : nums1) s1.add(x);
for (int x : nums2) s2.add(x);
for (int x : nums3) s3.add(x);
List<Integer> res =new ArrayList<>();
for (int x = 1; x <= 100; ++x) {
int cnt = 0;
if (s1.contains(x)) cnt++;
if (s2.contains(x)) cnt++;
if (s3.contains(x)) cnt++;
if (cnt >= 2) res.add(x);
}
return res;
}
}
1
2
3
deftwoOutOfThree(nums1, nums2, nums3):
s1, s2, s3 = set(nums1), set(nums2), set(nums3)
return [x for x in range(1, 101) if (x in s1) + (x in s2) + (x in s3) >=2]