Get the location of array elements using the BinarySearch method.
Set a string array −
string[] str = { "a", "m", "i", "t"};Now get the location of character ‘t’ using Array.BinarySearch −
Array.BinarySearch(str, "t");
Here is the complete code −
Example
using System;
using System.Text;
public class Demo {
public static void Main() {
string[] str = { "a", "m", "i", "t"};
// Using BinarySearch method to get location of character 't'
int res = Array.BinarySearch(str, "t");
// displaying the location
Console.WriteLine("Index : "+res);
}
}Output
Index : 3