diff --git a/solutions/java/gotta-snatch-em-all/1/src/main/java/GottaSnatchEmAll.java b/solutions/java/gotta-snatch-em-all/1/src/main/java/GottaSnatchEmAll.java new file mode 100644 index 00000000..89f861e9 --- /dev/null +++ b/solutions/java/gotta-snatch-em-all/1/src/main/java/GottaSnatchEmAll.java @@ -0,0 +1,47 @@ +import java.util.List; +import java.util.Set; + +class GottaSnatchEmAll { + + static Set newCollection(List cards) { + return Set.copyOf(cards); + } + + static boolean addCard(String card, Set collection) { + return collection.add(card); + } + + static boolean canTrade(Set myCollection, Set theirCollection) { + boolean a = !myCollection.containsAll(theirCollection); + boolean b = !theirCollection.containsAll(myCollection); + return a && b; + } + + static Set commonCards(List> collections) { + if (collections == null || collections.isEmpty()) { + return Set.of(); + } + + Set common = Set.copyOf(collections.get(0)); + + for (int i = 1; i < collections.size(); i++) { + common = common.stream() + .filter(collections.get(i)::contains) + .collect(java.util.stream.Collectors.toUnmodifiableSet()); + } + + return common; + } + + static Set allCards(List> collections) { + List cards = new java.util.ArrayList<>(); + for (Set set : collections) { + for (String card : set) { + if (!cards.contains(card)) { + cards.add(card); + } + } + } + return Set.copyOf(cards); + } +}