[코테 풀이] Intersection of Two Arrays

시내·2024년 6월 29일

Q_349) Intersection of Two Arrays

출처 : https://leetcode.com/problems/intersection-of-two-arrays/

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        ArrayList<Integer> one = new ArrayList<>();
        ArrayList<Integer> two = new ArrayList<>();
        for(int q : nums1) {
            if(!one.contains(q)) one.add(q);
        }
        for(int p : nums2) {
            if(!two.contains(p)) two.add(p);
        }
        
        ArrayList<Integer> arr = new ArrayList<>();
        int i = 0;
        for(int a = 0; a < one.size(); a++){
            if(two.contains(one.get(a))) arr.add(one.get(a));
        }
        int[] answer = new int[arr.size()];
        for(int b = 0; b < arr.size(); b++){
            answer[i++] = arr.get(b);
        }
        return answer;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글