Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
var seen = new HashSet<string>();
seen.Add("cat");
seen.Add("dog");
bool added = seen.Add("cat");
Console.WriteLine(added);
Console.WriteLine(seen.Count);False 2
- Expecting
True 2:Addreports whether it added the value, so a repeat returnsfalsewhile the set stays at two values. - Expecting an exception, as with a dictionary's
Add: a set skips a repeat and returnsfalseinstead of throwing.
- What does
seen.Contains("dog")print after the same lines? - How would you use the result of
Addto find the first word that repeats in a list?