Skip to content

Commit

Permalink
Merge pull request #11 from hangindev/patch-1
Browse files Browse the repository at this point in the history
Add solution to "largest sum of any two elements"
  • Loading branch information
danieldelcore authored Oct 7, 2020
2 parents 5d86a20 + acfd388 commit eaba551
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 eaba551

Please sign in to comment.