mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Create Intersection
This commit is contained in:
37
DataStructures/HashMap/Hashing/Intersection
Normal file
37
DataStructures/HashMap/Hashing/Intersection
Normal file
@ -0,0 +1,37 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
public class Intersection {
|
||||
|
||||
public static ArrayList Main(int arr[],int arr2[]) {
|
||||
HashMap<Integer,Integer> hmap=new HashMap<>();
|
||||
HashMap<Integer,Integer> hmap2=new HashMap<>();
|
||||
for(int i=0;i<arr.length;i++) {
|
||||
if(hmap.containsKey(arr[i])) {
|
||||
int val=hmap.get(arr[i]);
|
||||
hmap.put(arr[i],val+1);
|
||||
}else
|
||||
hmap.put(arr[i],1);
|
||||
|
||||
}
|
||||
ArrayList<Integer> res=new ArrayList<>();
|
||||
for(int i=0;i<arr2.length;i++) {
|
||||
if(hmap.containsKey(arr2[i])&&hmap.get(arr2[i])>0) {
|
||||
int val=hmap.get(arr2[i]);
|
||||
hmap.put(arr2[i],val-1);
|
||||
res.add(arr2[i]);
|
||||
}
|
||||
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public Intersection() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user