Fastest way to compare a string with an array of strings in C#2.0

后端 未结 4 1497
南旧
南旧 2020-12-30 04:07

What is the fastest way to compare a string with an array of strings in C#2.0

4条回答
  •  不知归路
    2020-12-30 04:40

    If you are doing this many times with a single array, you should sort the array and binary search it:

    Array.Sort(array);
    int index = Array.BinarySearch(array, input);
    // if (index < 0) 
    //      does not exists, "items > ~index" are larger and "< ~index" are smaller
    // otherwise, "items > index" are larger and "< index" are smaller.
    

    Otherwise just check the whole array naively:

    bool exists = Array.IndexOf(array, input) >= 0;
    

提交回复
热议问题