[코테 풀이] Two Out of Three

시내·2024년 6월 29일

Q_2032) Two Out of Three

출처 : https://leetcode.com/problems/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.

class Solution {
    public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
        List<Integer> answer = new ArrayList<>();
        List<Integer> one = new ArrayList<>(), two = new ArrayList<>(), three = new ArrayList<>();
        for (int a : nums1) {
            if (!one.contains(a)) one.add(a);
        }
        for (int b : nums2) {
            if (!two.contains(b)) two.add(b);
        }
        for (int c : nums3) {
            if (!three.contains(c)) three.add(c);
        }
        for (int i = 0; i < one.size(); i++) {
            if (two.contains(one.get(i)) || three.contains(one.get(i))) answer.add(one.get(i));
        }
        for (int j = 0; j < two.size(); j++) {
            if ((one.contains(two.get(j)) || three.contains(two.get(j))) && !answer.contains(two.get(j)))
                answer.add(two.get(j));
        }
        for (int k = 0; k < three.size(); k++) {
            if ((one.contains(three.get(k)) || two.contains(three.get(k))) && !answer.contains(three.get(k)))
                answer.add(three.get(k));
        }
        return answer;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글