classSolution {publicint[] intersection(int[] nums1,int[] nums2) {Set<Integer> set =newHashSet<>();Arrays.sort(nums2); // binary search need sortedfor(int num : nums1){if(binarySearch(nums2, num))//num1's elements are in num2set.add(num);//store elements in num1 will not repeat }int i =0;int[] res =newint[set.size()];for(Integer num : set) res[i++] = num;return res; }publicbooleanbinarySearch(int[] nums,int target){int lo =0;int hi =nums.length-1;while(lo <= hi){int mid = (hi - lo)/2+ lo;if(nums[mid] == target) returntrue;if(nums[mid] > target) hi = mid -1;else lo = mid +1; }returnfalse; }}