Intersection of Two Arrays Solutions in Java
Number 349
Difficulty Easy
Acceptance 62.6%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/intersection-of-two-arrays/description//// Author : liuyubobobo/// Time : 2017-07-12import java.util.HashSet;/// Hash Set/// Time complexity: O(len(nums1) + len(nums2))/// Space Complexity: O(len(nums1))public class Solution {public int[] intersection(int[] nums1, int[] nums2) {HashSet<Integer> record = new HashSet<Integer>();for(int num: nums1)record.add(num);HashSet<Integer> resultSet = new HashSet<Integer>();for(int num: nums2)if(record.contains(num))resultSet.add(num);int[] res = new int[resultSet.size()];int index = 0;for(Integer num: resultSet)res[index++] = num;return res;}private static void printArr(int[] arr){for(int e: arr)System.out.print(e + " ");System.out.println();}public static void main(String[] args) {int[] nums1 = {1, 2, 2, 1};int[] nums2 = {2, 2};int[] res = (new Solution()).intersection(nums1, nums2);printArr(res);}}