Skip to content

Commit

Permalink
Add solution to "largest sum of any two elements"
Browse files Browse the repository at this point in the history
  • Loading branch information
hangindev authored Oct 1, 2020
1 parent 5d86a20 commit acfd388
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2525,7 +2525,25 @@ There's no answer here - yet! Suggest one by [creating a pull request 🙏](http
<summary>Show Answer 💡</summary>
<br>

There's no answer here - yet! Suggest one by [creating a pull request 🙏](https://github.com/danieldelcore/mega-interview-guide/pulls)
```javascript
function findLargestSum(arr) {
const [largest, secondLargest] = arr.reduce(
([largest, secondLargest], currentValue) => {
if (currentValue > largest) {
return [currentValue, largest];
}
if (currentValue > secondLargest) {
return [largest, currentValue];
}
return [largest, secondLargest];
},
[Number.MIN_VALUE, Number.MIN_VALUE]
);
return largest + secondLargest;
}

findLargestSum([7, 5, 3, -1, 4, 34, 8, 9, 6, 0]); // 43
```

</details>

Expand Down

0 comments on commit acfd388

Please sign in to comment.