Posting my approach to this problem for anyone that may be stuck.
Thanks,
CactusHatMan
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class GottaSnatchEmAll {
static Set<String> newCollection(List<String> cards) {
HashSet<String> cardCollection = new HashSet<>();
for(int i = 0; i < cards.size(); i++) {
if (!cardCollection.contains(cards.get(i))) {
cardCollection.add(cards.get(i));
}
}
return cardCollection;
}
static boolean addCard(String card, Set<String> collection) {
return collection.add(card);
}
static boolean canTrade(Set<String> myCollection, Set<String> theirCollection) {
if(myCollection.size() == 0 || theirCollection.size() == 0) {
return false;
}
if(myCollection.containsAll(theirCollection) || theirCollection.containsAll(myCollection)) {
return false;
}
for(String card : myCollection) {
if(!theirCollection.contains(card)) {
return true;
}
}
return false;
}
static Set<String> commonCards(List<Set<String>> collections) {
if(collections.size() == 1) {
return collections.get(0);
}
int numOfDecks = collections.size() - 1;
Set<String> commoners = new HashSet<>();
// Start comparision at second deck
int i = 1;
for(String card : collections.get(0)) {
// If any of the decks are empty we must return an empty set
while(i <= numOfDecks) {
if(!collections.get(i).contains(card)) {
break;
} else {
if(i == numOfDecks) {
commoners.add(card);
break;
}
}
i++;
// Reset i when we reach last deck and move on to next card
}
}
return commoners;
}
static Set<String> allCards(List<Set<String>> collections) {
Set<String> masterCollection = new HashSet<>();
for(Set<String> deck : collections) {
Iterator<String> deckIterator = deck.iterator();
while(deckIterator.hasNext()) {
masterCollection.add(deckIterator.next());
}
}
return masterCollection;
}
}