Skip to content

Commit ac6c6b2

Browse files
committed
Intersection of Array
1 parent e4e5a7c commit ac6c6b2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Hashing/InsertionOfArray.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package SummerTrainingGFG.Hashing;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
6+
/**
7+
* @author Vishal Singh */
8+
public class InsertionOfArray {
9+
static int intersectionArray(int[] a,int[] b,int aSize,int bSize){
10+
HashSet<Integer> set = new HashSet<>();
11+
for (int i = 0; i < aSize; i++) {
12+
set.add(a[i]);
13+
}
14+
int res = 0;
15+
for (int i = 0; i < bSize; i++) {
16+
if (set.contains(b[i])){
17+
res++;
18+
set.remove(b[i]);
19+
}
20+
}
21+
return res;
22+
}
23+
public static void main(String[] args) {
24+
int[] a = {10,15,20,15,30,30,5};
25+
int[] b = {30,5,30,80};
26+
System.out.println("Intersection array has - "+intersectionArray(a,b,a.length,b.length)+" elements");
27+
}
28+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Java Solution for Data Structures and Algorithms.
120120
* [Hash Map Implementation](Hashing/P_HashMap.java)
121121
* [Count Distinct Elements](Hashing/CountDistinctElements.java)
122122
* [Frequency Of Array Elements](Hashing/FrequencyOfArrayElements.java)
123+
* [Intersection Of Array](Hashing/InsertionOfArray.java)
123124

124125
### `NOTE: Raise an issue if any program doesn't work.`
125126
### `More Topics to be added soon`

0 commit comments

Comments
 (0)