mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Change filename for Intersection.java file (#3178)
Co-authored-by: Yang Libin <contact@yanglibin.info>
This commit is contained in:
@ -1,64 +0,0 @@
|
||||
package com.thealgorithms.datastructures.hashmap.hashing;
|
||||
/*
|
||||
* this is algo which implies common mathematical set theory concept
|
||||
* called intersection in which result is common values of both the sets
|
||||
* here metaphor of sets is HashMap
|
||||
|
||||
|
||||
Test Case:
|
||||
Scanner scn=new Scanner(System.in);
|
||||
int len =scn.nextInt();
|
||||
int arr[]=new int[len];
|
||||
int arr2[]=new int[len];
|
||||
|
||||
for(int i=0;i<2*len;i++) {
|
||||
|
||||
if(i<len)
|
||||
arr[i]=scn.nextInt();
|
||||
if(i>=len) {
|
||||
arr2[i-len]=scn.nextInt();
|
||||
}
|
||||
}
|
||||
System.out.println(Main(arr,arr2));
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
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() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.thealgorithms.datastructures.hashmap.hashing;
|
||||
|
||||
/*
|
||||
* this is algo which implies common mathematical set theory concept
|
||||
* called intersection in which result is common values of both the sets
|
||||
* here metaphor of sets is HashMap
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Intersection {
|
||||
|
||||
public static List<Integer> intersection(int[] arr1, int[] arr2) {
|
||||
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Map<Integer, Integer> cnt = new HashMap<>(16);
|
||||
for (int v : arr1) {
|
||||
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
|
||||
}
|
||||
List<Integer> res = new ArrayList<>();
|
||||
for (int v : arr2) {
|
||||
if (cnt.containsKey(v) && cnt.get(v) > 0) {
|
||||
res.add(v);
|
||||
cnt.put(v, cnt.get(v) - 1);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private Intersection() {
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user