I have a list of five strings. I want to see if the variable passed in is equal to any of these five strings. Is there a better way than doing an if/else or case statement?
-
What do you mean by list of five strings? Do you actually have a `List
` or `string[]` containing those five strings, or do you have just five hardcoded strings, etc? – BoltClock Aug 26 '11 at 14:37 -
2The fact that you're suggesting a `case` suggests in turn that these five strings are constants. If this is true, don't bother being 'elegant' - just use a `case`. – AakashM Aug 26 '11 at 14:42
-
@AakashM: I completely agree. – Andrew Hare Aug 26 '11 at 14:42
-
1It's not a duplicate. He is looking for the fastest way to compare strings, not whether to use if/else statements to do so. He put it up as an example of what he *already* knows how to do. If anything, it's a duplicate of http://stackoverflow.com/questions/1051276/fastest-way-to-compare-a-string-with-an-array-of-strings-in-c2-0 but I'm not 100% sure of that either – Michael Aug 26 '11 at 14:43
4 Answers
Assuming I understand what you mean, here's an ugly, but simple to understand method, and we put the strings in to a list:
var data = new List<string>();
// add some:
data.Add("Stack");
data.Add("Overflow");
data.Add("Is");
data.Add("Awesome");
string test = "Stack";
// does our list contain our test?
bool found = data.Contains(test);

- 38,257
- 10
- 78
- 128
-
2I believe a HashSet
as an auxiliary structure is much more effiencient since the Contains() method can use a constant time algorithm (hash function) rather than a linked list scan. – Philipp Schmid Aug 26 '11 at 16:59
I believe using a HashSet is much more efficient!
Using the test program shown below, I get the following results:
list time: 558 hash time: 150
list time: 150 hash time: 7
list time: 138 hash time: 7
list time: 138 hash time: 7
list time: 142 hash time: 3
list time: 138 hash time: 3
list time: 142 hash time: 3
list time: 142 hash time: 3
list time: 138 hash time: 7
list time: 223 hash time: 7
Yes, it probably is the worst-case scenario for the list, as my test is looking for the last entry in the sequence, but it makes the point. Contains() has to search linearly the entire linked list (that's what List is) which is O(n), where as the HashSet is using a hash function to find the key, which is a constant time operation O(1)
Here is my test program:
static void Main(string[] args)
{
var list = new List<string>();
var hash = new HashSet<string>();
for (int i = 0; i < 1000; i++)
{
list.Add(i.ToString());
hash.Add(i.ToString());
}
Stopwatch sw = new Stopwatch();
for (int j = 0; j < 10; j++)
{
sw.Start();
bool isFound1 = list.Contains("999");
sw.Stop();
var time1 = sw.Elapsed;
sw.Reset();
sw.Start();
bool isFound2 = hash.Contains("999");
sw.Stop();
var time2 = sw.Elapsed;
sw.Reset();
Console.WriteLine("list time: " + time1.Ticks);
Console.WriteLine("hash time: " + time2.Ticks);
Console.WriteLine();
}
}

- 5,778
- 5
- 44
- 66
Assuming the passed variable is also a string, you might want to look into this question. Just put your five strings in a string[]
(or List
, or anything that implements IEnumerable
for that matter) and call IEnumerable<string>.Contains
to see if it is equal to any of the strings. (note that this will not tell you which string it is equal to, but since you haven't actually asked for that...)
-
Michael, would that be more efficient thann if else, if so efficient in what way? Like number of lies of code etc.. – cheedep Aug 26 '11 at 14:44
-
1Well, it's still an if statement but it's way shorter [ if (arr.Contains("test")) {...} ] as to running time, memory etc - it's a framework method so I'm not sure. @cschooley linked an article regarding performance that includes this method in his answer. – Michael Aug 26 '11 at 14:48
Did you see this:
And this article compares performance of 4 different methods of doing the same: http://www.tkachenko.com/blog/archives/000682.html