All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2001_2100.s2032_two_out_of_three.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g2001_2100.s2032_two_out_of_three;

// #Easy #Array #Hash_Table #2022_05_25_Time_9_ms_(45.56%)_Space_47.4_MB_(38.52%)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution {
    public List twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
        Set ans = new HashSet<>();
        Set set1 = new HashSet<>();
        for (int i : nums1) {
            set1.add(i);
        }
        Set set2 = new HashSet<>();
        for (int i : nums2) {
            set2.add(i);
        }
        Set set3 = new HashSet<>();
        for (int i : nums3) {
            set3.add(i);
        }
        for (int j : nums1) {
            if (set2.contains(j) || set3.contains(j)) {
                ans.add(j);
            }
        }
        for (int j : nums2) {
            if (set1.contains(j) || set3.contains(j)) {
                ans.add(j);
            }
        }
        for (int j : nums3) {
            if (set1.contains(j) || set2.contains(j)) {
                ans.add(j);
            }
        }
        List result = new ArrayList<>();
        result.addAll(ans);
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy