Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HashSet<string>.Contains() method in case -insensitive mode?

Tags:

string

c#

hashset

How to use HashSet<string>.Contains() method in case -insensitive mode?

like image 577
Tasawer Khan Avatar asked Apr 19 '10 13:04

Tasawer Khan


People also ask

How do you check if a HashSet contains a string?

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.

Does HashSet have Contains method?

HashSet contains() Method in JavaHashSet. contains() method is used to check whether a specific element is present in the HashSet or not. So basically it is used to check if a Set contains any particular element. Parameters: The parameter element is of the type of HashSet.

How does HashSet contains method work?

The purpose of the contains method is to check if an element is present in a given HashSet. It returns true if the element is found, otherwise false. Whenever an object is passed to this method, the hash value gets calculated. Then, the corresponding bucket location gets resolved and traversed.

What is the time complexity of HashSet contains?

On average, the contains() of HashSet runs in O(1) time. Getting the object's bucket location is a constant time operation.


1 Answers

You can create the HashSet with a custom comparer:

HashSet<string> hs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);  hs.Add("Hello");  Console.WriteLine(hs.Contains("HeLLo")); 
like image 60
João Angelo Avatar answered Oct 07 '22 11:10

João Angelo