Created
January 2, 2022 11:58
-
-
Save janvichhabra/d7d53365ba442da88163347b1861e140 to your computer and use it in GitHub Desktop.
Birthday party
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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