We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void main(String[] args) {
// Create two sets
Set<Integer> setA = new HashSet<>();
setA.add(1);
setA.add(2);
setA.add(3);
setA.add(4);
setA.add(5);
setA.add(6);
Set<Integer> setB = new HashSet<>();
setB.add(2);
setB.add(3);
setB.add(4);
setB.add(5);
setB.add(6);
setB.add(7);
setB.add(8);
// Find the intersection of the two sets
Set<Integer> intersection = new HashSet<>(setA);
intersection.retainAll(setB);
// Print the number of elements in the intersection
System.out.println(intersection.size());
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Basics of Sets and Relations #2
You are viewing a single comment's thread. Return to all comments →
import java.util.Set; import java.util.HashSet;
public class IntersectionOfSets {
}