solution used HashSet


  • 0
    A

    public static int[] intersection(int[] nums1, int[] nums2) {
    if (nums1.length == 0 || nums2.length == 0) {
    return new int[0];
    }
    HashSet<Integer> hashSet = new HashSet<Integer>();
    for (int i = 0; i < nums2.length; i++) {
    for (int j = 0; j < nums1.length; j++) {
    if (nums1[j] == nums2[i]) {
    hashSet.add(nums1[j]);
    }
    }
    }
    int[] result = new int[hashSet.size()];
    int k = 0;
    for (Iterator iterator = hashSet.iterator(); iterator.hasNext();) {
    Integer integer = (Integer) iterator.next();
    result[k] = integer;
    k++;
    }
    return result;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.