Skip to content

Commit d43de68

Browse files
MatthewPalmer9MacBookProitsvinayak
authored
Add FindSecondLargestSort (TheAlgorithms#523)
* Add FindSecondLargestSort * Add FindSecondLargestSort * Add FindSecondLargestSort * Add FindSecondLargestSort * Add FindSecondLargestSort * Add FindSecondLargestSort * Update and rename FindSecondLargestSort.js to FindSecondLargestElement.js * Update FindSecondLargestElement.js * Update FindSecondLargestElement.js Co-authored-by: MacBookPro <macbookpro@MacBookPros-MBP.attlocal.net> Co-authored-by: vinayak <itssvinayak@gmail.com>
1 parent b7ef904 commit d43de68

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Sorts/FindSecondLargestElement.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Find Second Largest is a real technical interview question.
3+
* Chances are you will be asked to find the second largest value
4+
* inside of an array of numbers. You must also be able to filter
5+
* out duplicate values. It's important to know how to do this with
6+
* clean code that is also easy to explain.
7+
*
8+
* Resources:
9+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
10+
*/
11+
12+
const secondLargestElement = (array) => {
13+
const largestElement = Math.max(...array)
14+
let element = 0
15+
16+
for (let i = 0; i < array.length; i++) {
17+
if (element < array[i] && array[i] !== largestElement) {
18+
element = array[i]
19+
}
20+
}
21+
22+
return element
23+
}
24+
25+
export { secondLargestElement }

0 commit comments

Comments
 (0)