Program to find the second largest odd number in C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1303
Let's say we have an array of integers like {78,92,44,63,71,97}. The objective is to find the second largest odd number. Below program will do so

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

var intCollection = new List<int>() { 78, 92, 44, 63, 71, 97 };
var secondLargetOdd = intCollection.Where(i => i % 2 != 0).OrderByDescending(o => o).Skip(1).Take(1).First();
Console.WriteLine("Second largest Odd Number is {0}", secondLargetOdd);
Console.ReadKey();
}
}
}

Result
---------
Second largest Odd Number is 71

Comments or Responses

Login to post response