Hi guys,
I have one question how to find third highest number in this string str="12,6,26,14,40";
can you give me sample code.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
FroglegRank #1,020 · 1.9K reputationPosted Mar 17, 2012, 10:10 PM EDT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace thirdHighest
{
class Program
{
static void Main(string[] args)
{
string str = "12,6,26,14,40";
string[] ss = str.Split(',');
int[] st2 = new int[ss.Length];
for (int i = 0; i < ss.Length; i++)
{
st2[i] = Convert.ToInt32(ss[i]);
}
Array.Sort(st2);
int answer = st2[st2.Length - 3];
}
}
}
SenthilkumarRank #387 · 6.5K reputationPosted Mar 18, 2012, 12:53 AM EDT
The .net introduced some latest concepts which allows to query the value and no need to iterate the element in the object.
The LINQ and Lambda expression allows us to write the expression.
VulpesRank #55 · 29.2K reputationPosted Mar 18, 2012, 4:26 AM EDT
Of course, it makes no difference in this example because, in a sequence of 5 numbers, the 3rd highest is also the 3rd lowest :)
Sam HobbsRank #107 · 20.5K reputationPosted Mar 18, 2012, 8:34 PM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 18, 2012, 9:18 PM EDT
also try
string str = "12,6,26,14,40";
var stringArray = str.Split(',');
var k = stringArray.OrderByDescending(i => int.Parse(i)).Skip(2).Take(1).FirstOrDefault();
hope this help.