From acfd3883fcd6444dc1dc5593eeb6c6d138dc6c80 Mon Sep 17 00:00:00 2001 From: Jason Leung Date: Thu, 1 Oct 2020 14:55:19 +0800 Subject: [PATCH] Add solution to "largest sum of any two elements" --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4115763..b43a020 100644 --- a/README.md +++ b/README.md @@ -2525,7 +2525,25 @@ There's no answer here - yet! Suggest one by [creating a pull request 🙏](http Show Answer 💡
-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 +```