From b24411bef70a705b2362ddb15761050c3c5c7805 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Fri, 17 Apr 2026 14:13:18 +0000 Subject: [PATCH] [Sync Iteration] java/gotta-snatch-em-all/1 --- .../1/src/main/java/GottaSnatchEmAll.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 solutions/java/gotta-snatch-em-all/1/src/main/java/GottaSnatchEmAll.java 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); + } +}