Find common elements in two arrays
Page 1 of 1
Find common elements in two arrays
If they are sorted:
public static void findCommon(int a[],int b[]){
for(int i=0,j=0;i<a.length&&j<b.length;){
if(a[i]==b[j]){
System.out.print(a[i]+" ");
i++;
j++;
}
else if(a[i]<b[j]){
i++;
}
else
j++;
}
}
if not, hashmap
public static void findCommon(int a[],int b[]){
for(int i=0,j=0;i<a.length&&j<b.length;){
if(a[i]==b[j]){
System.out.print(a[i]+" ");
i++;
j++;
}
else if(a[i]<b[j]){
i++;
}
else
j++;
}
}
if not, hashmap
yangwenzhou- Posts : 5
Join date : 2011-09-23
Similar topics
» Find Lowest common ancestor of bst
» Given a sorted array find all possible |ai - aj| where ai,aj belongs to Array A. n^2 is obvious. Find a solution in O(N).
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» Lowest Common Ancestor
» Deleting Elements from List
» Given a sorted array find all possible |ai - aj| where ai,aj belongs to Array A. n^2 is obvious. Find a solution in O(N).
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» Lowest Common Ancestor
» Deleting Elements from List
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|