Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajnenie;
- import java.util.LinkedHashSet;
- import java.util.Scanner;
- import java.util.Set;
- public class SetsOfElements {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read lengths of the two sets
- int n = scanner.nextInt();
- int m = scanner.nextInt();
- scanner.nextLine(); // Consume the newline character
- Set<Integer> set1 = new LinkedHashSet<>();
- Set<Integer> set2 = new LinkedHashSet<>();
- // Read N numbers for the first set
- for (int i = 0; i < n; i++) {
- int num = scanner.nextInt();
- set1.add(num);
- }
- // Read M numbers for the second set
- for (int i = 0; i < m; i++) {
- int num = scanner.nextInt();
- set2.add(num);
- }
- // Find the intersection of the two sets
- set1.retainAll(set2);
- // Print the common elements
- for (int num : set1) {
- System.out.print(num + " ");
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement