# Simple java solution

• ``````public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer>nums1Set = new HashSet<Integer>();
HashSet<Integer>nums2Set = new HashSet<Integer>();
HashSet<Integer>resSet = new HashSet<Integer>();

for (int i = 0; i < nums1.length; i++){
}
for (int i = 0; i < nums2.length; i++){
}
Iterator<Integer> iter = nums1Set.iterator();
while (iter.hasNext()){
Integer next = iter.next();
if (nums2Set.contains(next)){
}
}
int[]res = new int[resSet.size()];
Iterator<Integer>resIterator = resSet.iterator();
int index = 0;
while(resIterator.hasNext()){
res[index] = resIterator.next();
index++;
}
return res;
}
}``````

• ``````import java.util.Arrays;

public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int length = 0;
int number =0;
if ((nums1.length != 0) && (nums2.length != 0)) {
for (int i = 0; i < nums1.length; i++) {
boolean valid = true;
if ((i != 0) && (nums1[i-1] == nums1[i])) {
valid = false;
}
if (binarySearch(nums2, nums1[i], 0, nums2.length-1) && valid) {
length++;
}
}
}
int[] nums3 = new int[length];
if ((nums1.length != 0) && (nums2.length != 0)) {
for (int i = 0; i < nums1.length; i++) {
boolean valid = true;
if ((i != 0) && (nums1[i-1] == nums1[i])) {
valid = false;
}
if (binarySearch(nums2, nums1[i], 0, nums2.length-1) && valid) {
nums3[number] = nums1[i];
number++;
}
}
}
return nums3;
}
public static boolean binarySearch(int[] sortedArr, int target, int start, int end) {

if (start > end - 1 && (sortedArr[(start+end)/2] != target)) {
return false;
}
if (sortedArr[(start+end)/2] == target) {
return true;
}
else if (sortedArr[(start+end)/2] < target) {
start = (start+end)/2 + 1;
}
else {
end = (start+end)/2 - 1;
}
return binarySearch(sortedArr, target, start, end);
}
}``````

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