Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the element from hashset where hashset.Count==1

Tags:

c#

How to get the element from hashset that is known to contain exactly 1 element? (without iterating it)

like image 985
william007 Avatar asked Aug 03 '12 09:08

william007


People also ask

How do you count elements in HashSet?

HashSet. size() method is used to get the size of the HashSet or the number of elements present in the HashSet. Parameters: This method does not takes any parameter. Return Value: The method returns the size or the number of elements present in the HashSet.

Is searching a HashSet O 1?

Yes, accessing and finding an element in HashSet is O(1). HashSet stores elements using the hashing technique. Another important property of HashSet is that it contains only unique elements. For example you can check if the element already exists in HashSet.

How do you check if a value is in a HashSet?

The contains() method of Java HashSet class is used to check if this HashSet contains the specified element or not. It returns true if element is found otherwise, returns false.


1 Answers

You could use Single()

var element = yourHashSet.Single();
like image 135
juergen d Avatar answered Sep 20 '22 13:09

juergen d