clueless coding // TODO: be smarter

LeetCode Intersection of Two Arrays: Java Solution



TL;DR Code Solution


public int[] intersection(int[] nums1, int[] nums2) {

    HashSet<Integer> set = new HashSet<Integer>();
    List<Integer> list = new ArrayList<Integer>();


    for (int num : nums1) {
        set.add(num);
    }

    for (int num : nums2) {
        if (set.contains(num)) {
            list.add(num);
            set.remove(num);
        }
    }

    int[] arr = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
        arr[i] = list.get(i);
    }
    return arr;




}