When to use a hashset | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When to use a hashset

I am just discovering the hashset in C#. When would one use a hashset and do you want to know if an item is not added to the list. I was thinking about an article list, where it is not allowed to have duplicate items. In this case you need to send feedback to the user prompted him to use another name. Could you do this with a hashset ? Is it preferred ? https://code.sololearn.com/cuGaGplYCj6E

21st Feb 2020, 9:41 PM
sneeze
sneeze - avatar
1 Answer
+ 1
When you adding item to hash Set it return true if it is successful. And returns false if is unsuccessful or already there.. So you can check by if condition and report accordingly.. Edit: oh.. you already know it.. HashSet is preferred when you don't want to allow dublicate..
21st Feb 2020, 10:05 PM
Jayakrishna 🇮🇳