Skip to content

Instantly share code, notes, and snippets.

@janvichhabra
Created January 2, 2022 11:58
Show Gist options
  • Save janvichhabra/d7d53365ba442da88163347b1861e140 to your computer and use it in GitHub Desktop.
Save janvichhabra/d7d53365ba442da88163347b1861e140 to your computer and use it in GitHub Desktop.
Birthday party
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n1 = scn.nextInt();
int[] a1 = new int[n1];
for (int i = 0; i < n1; i++) {
a1[i] = scn.nextInt();
}
int n2 = scn.nextInt();
int[] a2 = new int[n2];
for (int i = 0; i < n2; i++) {
a2[i] = scn.nextInt();
}
System.out.println(intersection(a1,a2));
}
public static int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set1 = new HashSet<Integer>();
for (Integer n : nums1) set1.add(n);
HashSet<Integer> set2 = new HashSet<Integer>();
for (Integer n : nums2) set2.add(n);
set1.retainAll(set2);
int [] output = new int[set1.size()];
int idx = 0;
for (int s : set1) output[idx++] = s;
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment