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)
Union:
1) Initialize union U as empty.
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.
Intersection:
1) Initialize intersection I as empty.
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.
2) Use O(m + n) algorithms to find union and intersection of two sorted arrays.
Overall time complexity of this method is O(mLogm + nLogn).
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.
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.
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.
public class IntersectUnionArray { private int binarySearch(int[] A, int target) { int start = 0, end = A.length-1; while(start <= end) { int m = (start+end)/2; if(A[m] == target) { return m; } else if(A[m] < target) { start = m+1; } else { end = m-1; } } return -1; } public List<Integer> unionArray(int[] A, int[] B) { if(A.length > B.length) { return unionArray(B, A); } Arrays.sort(A); List<Integer> list = new ArrayList<>(); for(int num:A) { list.add(num); } for(int num:B) { if(binarySearch(A, num) == -1) { list.add(num); } } return list; } public List<Integer> intersectArray(int[] A, int[] B) { if(A.length > B.length) { return intersectArray(B, A); } Arrays.sort(A); List<Integer> list = new ArrayList<>(); for(int num:B) { if(binarySearch(A, num) != -1) { list.add(num); } } return list; } public static void main(String[] args) { int[] A = {7, 1, 5, 2, 3, 6}; int[] B = {3, 8, 6, 20, 7}; IntersectUnionArray iu = new IntersectUnionArray(); System.out.println(iu.unionArray(A, B)); System.out.println(iu.intersectArray(A, B)); } }
Method 4 (Use Hashing)
Union:
1) Initialize union U as empty.
1) Initialize an empty hash table.
2) Iterate through first array and put every element of first array in the hash table, and in U.
4) For every element x of second array, do following
…….a) Search x in the hash table. If x is not present, then copy it to U.
5) Return U.
Intersection:
1) Initialize intersection I as empty.
2) In initialize an empty hash table.
3) Iterate through first array and put every element of first array in the hash table.
4) For every element x of second array, do following
…….a) Search x in the hash table. If x is present, then copy it to I.
5) Return I.
Time complexity of this method is Θ(m+n) under the assumption that hash table search and insert operations take Θ(1) time.
Reference:
http://www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/
相关推荐
350. 两个数组的交集II Intersection of Two Arrays II思路一:排序找交集,从小到大遍历,遇到相等的就加到ans中,然后都看下一
标题 "Intersection-of-Two-Arrays-LeetCode" 指向的是一个关于LeetCode上的编程问题,该问题要求找出两个整数数组的交集。在Java编程语言中解决此类问题涉及数组处理、集合操作以及算法设计。以下是这个题目相关的...
leetcode中国大批 ...Intersection of the two sorted arrays. 7. Write a program to cyclically rotate an array by one. 8. find Largest sum contiguous Subarray [V. IMP] 9* Minimise the maximum
leetcode中国Final450_数据结构 实际上,这个存储库包含...Intersection of the two sorted arrays. *Write a program to cyclically rotate an array by one. *find Largest sum contiguous Subarray [V. IMP] *Minimi
《Smarter Decisions–The Intersection of Internet of Things and Decision Science》这本书,旨在结合物联网与决策科学的优势,通过数据科学的力量,帮助读者深入理解这一交汇点,进而能在相关项目中做出更加明智...
leetcode ...and last position of element in sorted array 300: longest increasing subsequence hard 154: find minimum in rotated sorted array ii 315: count of smaller numbers after self 3
javascript js_leetcode题解之160-intersection-of-two-linked-lists.js
python python_leetcode题解之160_Intersection_of_Two_Linked_Lists.py
SIMD Compression and the Intersection of Sorted IntegersDaniel Lemire LICEF Research CenterTELUQ, Université du Québec5800 Saint-DenisMontreal, QC H2S 3L5 Can.lemire@gmail.comLeonid Boytsov ...
Functions: (1)The Union of two Sets (overload operator +,+=) (2)The Intersection of two Sets (overload operator *,*=) (3)The Difference of two Sets (overload operator -) (4)Add an element to a Set (5)...
A.1 The Intersection of Two Vector Spaces 459 A.2 The Sum of Two Vector Spaces 460 A.3 The Cartesian Product of Two Vector Spaces 461 A.4 The Tensor Product of Two Vector Spaces 461 A.5 The Kronecker ...
### 参数曲面与平面相交算法 #### 概述 在计算机图形学和几何建模领域,参数曲面与平面的相交问题是一项基础而重要的任务。由Randy B. Lee与David A....该算法通过利用基本的微分几何分析来实现高效率、低内存消耗,...
两个大数组的交集
Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life Find All Numbers Disappeared in an Array Shortest Unsorted Continuous Subarray Rotate Image 宝石与石头Jewels and Stones Kids ...
Functions: (1)The Union of two Sets (overload operator , =) (2)The Intersection of two Sets (overload operator *,*=) (3)The Difference of two Sets (overload operator -) (4)Add an element to a Set ...
Functions: (1)The Union of two Sets (overload operator +,+=) (2)The Intersection of two Sets (overload operator *,*=) (3)The Difference of two Sets (overload operator -) (4)Add an element ...
信息安全_数据安全_Intersection of Data Breach Noti 常规渗透 可信编译 态势感知 安全审计 企业安全
find_intersection 函数接收两个不同数据集的 x 和 y 坐标。 它使用线性插值来增加每组数据点的数量。 然后,它在用户定义的置信度(例如:1或2%)内找到交点。 该函数返回交点的 x 和 y 坐标以及一个表示交点...