Skip to content

Commit b1edea3

Browse files
committed
Add intersection of arrays
1 parent 7bcd898 commit b1edea3

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ List of Programs related to data structures and algorithms
6464
| 23 | Disappeared numbers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.md) | Easy | Array traversal |
6565
| 24 | Identical pairs | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.md) | Easy | Array traversal & map |
6666
| 25 | Destination City | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.md) | Easy | Array traversal & set |
67-
| 26 | Set mismatch | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.setMismatch/setMismatch.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.md) | Easy | Array traversal & marking numbers |
67+
| 26 | Set mismatch | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.md) | Easy | Array traversal & marking numbers |
68+
| 27 | Intersection of arrays | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/27.intersectionOfArrays/intersectionOfArrays.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/27.intersectionOfArrays/intersectionOfArrays.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/27.intersectionOfArrays/intersectionOfArrays.md) | Easy | Set and array traversal |
6869

6970
### String
7071

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package java1.algorithms.array.intersectionOfArrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class IntersectionOfArrays {
9+
private static List<Integer> intersection(int[] nums1, int[] nums2){
10+
Set<Integer> set = new HashSet<>();
11+
List<Integer> result = new ArrayList<>();
12+
13+
for (Integer num : nums1) {
14+
set.add(num);
15+
}
16+
17+
for (Integer num : nums2) {
18+
if(set.contains(num)) {
19+
result.add(num);
20+
set.remove(num);
21+
}
22+
}
23+
24+
return result;
25+
}
26+
27+
public static void main(String[] args) {
28+
int[] nums1 = new int[]{5,3,3,5}, nums2 = new int[]{5,3};
29+
int[] nums3 = new int[]{1, 3, 5}, nums4 = new int[]{2,4};
30+
System.out.println(intersection(nums1, nums2));
31+
System.out.println(intersection(nums3, nums4));
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
**Description:**
2+
Given two integer arrays `nums1` and `nums2`, return an array of their intersection in such away that each element in the result must be unique.
3+
4+
**Note:** The elements in result can appear in any order.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums1 = [5,3,3,5], nums2 = [5,3]
10+
Output: [ 5, 3 ]
11+
12+
Example 2:
13+
14+
Input: nums1 = [1, 3, 5], nums2 = [2, 4]
15+
Output: [ ]
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of set data structure and array traversal over the elements. The algorithmic approach can be summarized as follows:
19+
20+
1. Create a function named `intersection`, which accepts input arrays(`nums1` and `nums2`) to find the intersection of elements.
21+
22+
2. Define a set data structure named `set` which stores the elements of `nums1`. Create a result list(`result`) to return the intersection of elements.
23+
24+
3. Iterate over first array(`nums1`) and add each element to a set.
25+
26+
4. Iterate over second array(`nums2`) and add an element to result list if that element exists in a set. Also, remove the same element from set to avoid adding duplicate elements to result list.
27+
28+
5. Return the result array which has intersection of elements.
29+
30+
**Time and Space complexity:**
31+
This algorithm has a time complexity of `O(n1+n2)`, where `n1` is the number of elements in first array and `n2` is the number of elements in second array. This is because we need to iterate over all the elements of first array to insert them into a set with a time complexity of `O(n1)`. At the same time, we need to iterate over each element of second array to find the intersected elements. Hence, total time complexity is `O(n1+n2)`.
32+
33+
It takes constant time complexity of `O(n1+n2)`. This is due to hash set usage with a space complexity of `O(n1)`and `O(min(n1+n2))` if all the elements of one array exists in another array.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function intersection(nums1, nums2) {
2+
const set = new Set(nums1);
3+
const result = [];
4+
5+
for (const num of nums2) {
6+
if(set.has(num)) {
7+
result.push(num);
8+
set.delete(num);
9+
}
10+
}
11+
12+
return result;
13+
}
14+
15+
const nums1 = [5,3,3,5], nums2 = [5,3];
16+
const nums3 = [1, 3, 5], nums4 = [2,4];
17+
console.log(intersection(nums1, nums2));
18+
console.log(intersection(nums3, nums4));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
**Description:**
2+
Given two integer arrays `nums1` and `nums2`, return an array of their intersection in such away that each element in the result must be unique.
3+
4+
**Note:** The elements in result can appear in any order.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums1 = [5,3,3,5], nums2 = [5,3]
10+
Output: [ 5, 3 ]
11+
12+
Example 2:
13+
14+
Input: nums1 = [1, 3, 5], nums2 = [2, 4]
15+
Output: [ ]
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of set data structure and array traversal over the elements. The algorithmic approach can be summarized as follows:
19+
20+
1. Create a function named `intersection`, which accepts input arrays(`nums1` and `nums2`) to find the intersection of elements.
21+
22+
2. Define a set data structure named `set` which stores the elements of `nums1`. Create a result list(`result`) to return the intersection of elements.
23+
24+
3. Iterate over first array(`nums1`) and add each element to a set.
25+
26+
4. Iterate over second array(`nums2`) and add an element to result list if that element exists in a set. Also, remove the same element from set to avoid adding duplicate elements to result list.
27+
28+
5. Return the result array which has intersection of elements.
29+
30+
**Time and Space complexity:**
31+
This algorithm has a time complexity of `O(n1+n2)`, where `n1` is the number of elements in first array and `n2` is the number of elements in second array. This is because we need to iterate over all the elements of first array to insert them into a set with a time complexity of `O(n1)`. At the same time, we need to iterate over each element of second array to find the intersected elements. Hence, total time complexity is `O(n1+n2)`.
32+
33+
It takes constant time complexity of `O(n1+n2)`. This is due to hash set usage with a space complexity of `O(n1)`and `O(min(n1+n2))` if all the elements of one array exists in another array.

0 commit comments

Comments
 (0)