using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
List<string> lista = new List<string>() { "aoo", "bar", "baz" };
Console.WriteLine(lista.BinarySearch("bar")); // Está na lista (posição 1)
Console.WriteLine(lista.BinarySearch("bay")); // Não na lista (negativo)
Console.WriteLine(~lista.BinarySearch("bay")); // Se for inserir, insira na posição 2
Console.WriteLine();
List<string> lista2 = new List<string>() { "a", "b", "b", "c", "c", "c", "d", "d", "d", "d" };
Console.WriteLine(lista2.BinarySearch("a")); // 0 (o único)
Console.WriteLine(lista2.BinarySearch("b")); // 1 (o primeiro)
Console.WriteLine(lista2.BinarySearch("c")); // 4 (o segundo)
Console.WriteLine(lista2.BinarySearch("d")); // 7 (o segundo)
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKCnB1YmxpYyBjbGFzcyBUZXN0CnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKCXsKCQlMaXN0PHN0cmluZz4gbGlzdGEgPSBuZXcgTGlzdDxzdHJpbmc+KCkgeyAiYW9vIiwgImJhciIsICJiYXoiIH07CgkJCgkJQ29uc29sZS5Xcml0ZUxpbmUobGlzdGEuQmluYXJ5U2VhcmNoKCJiYXIiKSk7IC8vIEVzdMOhIG5hIGxpc3RhIChwb3Npw6fDo28gMSkKCQkKCQlDb25zb2xlLldyaXRlTGluZShsaXN0YS5CaW5hcnlTZWFyY2goImJheSIpKTsgLy8gTsOjbyBuYSBsaXN0YSAobmVnYXRpdm8pCgkJQ29uc29sZS5Xcml0ZUxpbmUofmxpc3RhLkJpbmFyeVNlYXJjaCgiYmF5IikpOyAvLyBTZSBmb3IgaW5zZXJpciwgaW5zaXJhIG5hIHBvc2nDp8OjbyAyCgkJCgkJQ29uc29sZS5Xcml0ZUxpbmUoKTsKCQlMaXN0PHN0cmluZz4gbGlzdGEyID0gbmV3IExpc3Q8c3RyaW5nPigpIHsgImEiLCAiYiIsICJiIiwgImMiLCAiY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