Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insertion_two_array.java file created #378

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions DSA/Java/Arrays/Insertion_two_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class Insertion_two_array {
public int[] intersection(int[] nums1, int[] nums2) {
Arrays.sort(nums2);
Set<Integer> set = new HashSet<Integer>();
for(int num : nums1){
if(binarySearch(nums2, num))
set.add(num);
}

int i = 0;
int[] res = new int[set.size()];
for(Integer num : set){
res[i++] = num;
}
return res;
}

//using binary Search

private boolean binarySearch(int[] num, int x){
int high = num.length - 1;
int low = 0;

while(low <= high){
int mid = low + (high - low)/2;
if(x < num[mid]){
high = mid - 1;
} else if(x > num[mid]){
low = mid + 1;
} else {
return true;
}
}

return false;
}
}