Find the 3 largest elements in the Array

Find the 3 largest elements in the Array

ยท

1 min read

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)

ย