Skip to main content

Command Palette

Search for a command to run...

Find the 3 largest elements in the Array

Published
•1 min read•View as Markdown
Find the 3 largest elements in the Array
T

Firestone

Simple Approach will update soon with the newly learnt approach. :)

let arr = [1,2,3,3,4,5,4,6,7,7,7,8,9,10,10,10,10]

let n = arr.length

function thirdLargest (arr,n){

let first, second , third ;

second = first = third = Number.MIN_VALUE

if(n<3)

{ console.log("Invaild on the given array") }

else

{ console.log("let's rock n roll")}

for (let i = 0; i<=n ; i++ )

if (arr[i] > first){

third = second

second = first

first = arr[i]

}

else if (arr[i] > second && arr[i] != first){

third = second

second = arr[i]

}

else if(arr[i] > third && arr[i] != second){

first = arr[i]

}

console.log(first,second,third) }

thirdLargest(arr,n)

More from this blog

Top Gun Maping

14 posts