Find Union and Intersection of two unsorted arrays - GeeksforGeeks
Find Union and Intersection of two unsorted arrays Given two unsorted arrays that represent two sets (elements in every array are distinct), find union and intersection of two arrays. For example, if the input arrays are: arr1[] = {7, 1, 5, 2, 3, 6} arr2[] = {3, 8, 6, 20, 7} Then your program should print Union as {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6}. Note that the elements of union and intersection can be printed in any order. Method 1 (Naive) 2) Copy all elements of first array to U. 3) Do following for every element x of second array: …..a) If x is not present in first array, then copy x to U. 4) Return U. 2) Do following for every element x of first array …..a) If x is present in second array, then copy x to I. 4) Return I. Time complexity of this method is O(mn) for both operations. Here m and n are number of elements in arr1[] and arr2[] respectively. Method 2 (Use Sorting) 1) Sort arr1[] and arr2[]. This step takes O(mLogm + nLogn) time.
排序二分搜索
将较短的那个集合排序,然后对于较长的集合中每一个元素,都在较短的集合中二分搜索相应的元素。如果找到则加入结果中。之所以选择对较短的集合二分搜索,是因为排序需要
将第一个集合中的元素加入一个哈希表中,然后过一遍第二个集合,看是否存在于第一个集合中,因为用了哈希,所以总时间只要O(N)。
排序归并法
暴力解法,对于每个在集合1中的元素,我们遍历一遍集合2看看是否存在,如果存在则是Intersection。
将两个集合合并起来排序,这样如果排序后的数组中有两个相同的元素,说明就是Intersection。
Find Union and Intersection of two unsorted arrays Given two unsorted arrays that represent two sets (elements in every array are distinct), find union and intersection of two arrays. For example, if the input arrays are: arr1[] = {7, 1, 5, 2, 3, 6} arr2[] = {3, 8, 6, 20, 7} Then your program should print Union as {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6}. Note that the elements of union and intersection can be printed in any order. Method 1 (Naive) 2) Copy all elements of first array to U. 3) Do following for every element x of second array: …..a) If x is not present in first array, then copy x to U. 4) Return U. 2) Do following for every element x of first array …..a) If x is present in second array, then copy x to I. 4) Return I. Time complexity of this method is O(mn) for both operations. Here m and n are number of elements in arr1[] and arr2[] respectively. Method 2 (Use Sorting) 1) Sort arr1[] and arr2[]. This step takes O(mLogm + nLogn) time.
Method 3 (Use Sorting and Searching)
Union:
1) Initialize union U as empty.
2) Find smaller of m and n and sort the smaller array.
3) Copy the smaller array to U.
4) For every element x of larger array, do following
…….b) Binary Search x in smaller array. If x is not present, then copy it to U.
5) Return U.
Union:
1) Initialize union U as empty.
2) Find smaller of m and n and sort the smaller array.
3) Copy the smaller array to U.
4) For every element x of larger array, do following
…….b) Binary Search x in smaller array. If x is not present, then copy it to U.
5) Return U.
Intersection:
1) Initialize intersection I as empty.
2) Find smaller of m and n and sort the smaller array.
3) For every element x of larger array, do following
…….b) Binary Search x in smaller array. If x is present, then copy it to I.
4) Return I.
1) Initialize intersection I as empty.
2) Find smaller of m and n and sort the smaller array.
3) For every element x of larger array, do following
…….b) Binary Search x in smaller array. If x is present, then copy it to I.
4) Return I.
Time complexity of this method is min(mLogm + nLogm, mLogn + nLogn) which can also be written as O((m+n)Logm, (m+n)Logn). This approach works much better than the previous approach when difference between sizes of two arrays is significant.
Find Intersection of Two Sets排序二分搜索
将较短的那个集合排序,然后对于较长的集合中每一个元素,都在较短的集合中二分搜索相应的元素。如果找到则加入结果中。之所以选择对较短的集合二分搜索,是因为排序需要
NlogN
的时间,如果对较长数组排序,假设N>M
,则时间复杂度是NlogN+MlogN
,而对较短数组排序,时间为MlogM+NlogM
,显然(M+N)logN > (M+N)logM
public List<Integer> findByBinarySearch(int[] arr1, int[] arr2){
List<Integer> res = new LinkedList<Integer>();
if(arr1.length > arr2.length){
int[] tmp = arr1;
arr1 = arr2;
arr2 = tmp;
}
Arrays.sort(arr1);
for(int i = 0;i < arr2.length; i++){
if(binarySearch(arr1, arr2[i])){
res.add(arr2[i]);
}// else do nothing
}
return res;
}
private boolean binarySearch(int[] arr, int target){
int min = 0, max = arr.length - 1;
while(min <= max){
int mid = min + (max - min) / 2;
if(arr[mid] == target){
return true;
} else if (arr[mid] > target) {
max = mid - 1;
} else {
min = mid + 1;
}
}
return false;
}
哈希表法将第一个集合中的元素加入一个哈希表中,然后过一遍第二个集合,看是否存在于第一个集合中,因为用了哈希,所以总时间只要O(N)。
public List<Integer> findByHashmap(int[] arr1, int[] arr2){
List<Integer> res = new LinkedList<Integer>();
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0; i < arr1.length; i++){
set.add(arr1[i]);
}
for(int i = 0; i < arr2.length; i++){
if(set.contains(arr2[i])){
res.add(arr2[i]);
}
}
return res;
}
public List<Integer> findBySortingAndMerge(int[] arr1, int[] arr2){
Arrays.sort(arr1);
Arrays.sort(arr2);
List<Integer> res = new LinkedList<Integer>();
int i = 0, j = 0;
while(i < arr1.length && j < arr2.length){
if (arr1[i] == arr2[j]){
res.add(arr1[i]);
i++;
j++;
} else if (arr1[i] < arr2[j]){
i++;
} else if (arr1[i] > arr2[j]){
j++;
}
}
return res;
}
暴力解法,对于每个在集合1中的元素,我们遍历一遍集合2看看是否存在,如果存在则是Intersection。
public List<Integer> findByBruteForce(int[] arr1, int[] arr2){
List<Integer> res = new LinkedList<Integer>();
for(int i = 0; i < arr1.length; i++){
for(int j = 0; j < arr2.length; j++){
if(arr1[i] == arr2[j]){
res.add(arr1[i]);
}
}
}
return res;
}
统一排序法将两个集合合并起来排序,这样如果排序后的数组中有两个相同的元素,说明就是Intersection。
public List<Integer> findBySortTogether(int[] arr1, int[] arr2){
List<Integer> res = new LinkedList<Integer>();
int[] all = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, all, 0, arr1.length);
System.arraycopy(arr2, 0, all, arr1.length, arr2.length);
Arrays.sort(all);
for(int i = 0; i < all.length - 1; i++){
if(all[i] == all[i + 1]){
res.add(all[i]);
i++;
}
}
return res;
}
http://www.cnblogs.com/EdwardLiu/p/5144654.html 求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4]
difference
5 public ArrayList<Integer> setInters(int[] arr1, int[] arr2) { 6 int i1=0, i2=0; 7 ArrayList<Integer> res = new ArrayList<Integer>(); 8 while (i1<arr1.length && i2<arr2.length) { 9 if (arr1[i1] < arr2[i2]) i1++; 10 else if (arr1[i1] > arr2[i2]) i2++; 11 else { 12 res.add(arr1[i1]); 13 i1++; 14 i2++; 15 } 16 } 17 return res; 18 } 19 20 public ArrayList<Integer> setDiff(int[] arr1, int[] arr2) { 21 int i1=0, i2=0; 22 ArrayList<Integer> res = new ArrayList<Integer>(); 23 while (i1<arr1.length && i2<arr2.length) { 24 if (arr1[i1] < arr2[i2]) { 25 res.add(arr1[i1]); 26 i1++; 27 } 28 else if (arr1[i1] > arr2[i2]) { 29 i2++; 30 } 31 else { 32 i1++; 33 i2++; 34 } 35 } 36 while (i1 < arr1.length) { 37 res.add(arr1[i1]); 38 i1++; 39 } 40 return res; 41 }Read full article from Find Union and Intersection of two unsorted arrays - GeeksforGeeks