I have a List containing a lot of paths. I have a specific path I want to check against this list to see if there are any paths there that uses this path, ie:
f.StartsWith(r.FILENAME) && f != r.FILENAME
What would be the fastest way of doing this?
edit: Complete function from answer below:
static bool ContainsFragment(string[] paths, string fragment) { // paths **must** be pre-sorted via Array.Sort(paths); if (paths.Length == 0) return false; int index = Array.BinarySearch(paths, fragment); if (index >= 0 && index+1 < paths.Length) { //we found it if (paths[index + 1].StartsWith(fragment) && paths[index + 1].EndsWith('.manifest')) { return true; } } return false; }
The fastest way is probably with binary search:
But the cost of sorting the array will outweigh any benefit if you are only doing it a few times; in which case, just scan the array – either with LINQ etc, or just: