Leaders in an array
static void Main(string[] args)
{
int[] leaders = new int[] { 5, 6, 7, 0, 1, 3, 2 };
foreach (var item in GetLeaders(leaders))
{
Console.WriteLine(item);
}
Console.Read();
}
static List<int> GetLeaders(int[] array)
{
List<int> lstLeaders = new List<int>();
for (int i = array.Length - 1; i >= 0; i--)
{
bool add = true;
for (int j = i; j < array.Length; j++)
{
if (array[i] < array[j])
{
add = false;
break;
}
}
if (add || i==(array.Length - 1))
{
lstLeaders.Add(array[i]);
}
}
return lstLeaders;
}