0

I would like to know if there is a shorter hand of doing what I have done :

public bool IsDeckUnique()
        {
            foreach (Card card1 in DeckOfCards)
            {
                foreach (Card card2 in DeckOfCards)
                {
                    if (card1.Equals(card2))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

The .Equals just checks if the suit and value of a card is the same if so returns true. I basically want to know if there is a form of way to shorten down the 2 foreach statments, thank you

  • [Check for any element that exists in two collections](https://stackoverflow.com/a/12584194) – 001 May 12 '21 at 18:42
  • The only practical difference between your code and the Linq code in the duplicate answer is that the Linq code creates and fills a set from one of the lists to make searches faster. If you did that, your code would be pretty much equivalent to the Linq solution. – D Stanley May 12 '21 at 19:00
  • @DStanley so in your opinion, would my code be fine, and there isnt a much need to shorten it down? thanks for the response – xanderdaily101 May 12 '21 at 19:09
  • I think it's fine - it stops when it finds a first match, which is good. The overhead of creating a set may be more than the benefit of faster searches. You'd have to try it both ways and compare multiple scenarios (first card matches, last card matches, middle card matches, no matches) to be certain. – D Stanley May 12 '21 at 19:50
  • Also I realized that the "duplicate" is not really a duplicate. That question is checking for list equivalence, while you want _any_ overlap. The link in the first comment is a much better duplicate. I've fixed that. – D Stanley May 12 '21 at 19:52
  • @DStanley thanks for the help! think i will stick to what i got for now – xanderdaily101 May 12 '21 at 20:51

0 Answers0