From 9da3f8ca716e5748db1e22f7abc864d2ee0d9d36 Mon Sep 17 00:00:00 2001 From: MacBookPro Date: Mon, 26 Oct 2020 20:29:18 -0500 Subject: [PATCH 1/9] Add FindSecondLargestSort --- Sorts/FindSecondLargestSort.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Sorts/FindSecondLargestSort.js diff --git a/Sorts/FindSecondLargestSort.js b/Sorts/FindSecondLargestSort.js new file mode 100644 index 0000000000..0ec1432af1 --- /dev/null +++ b/Sorts/FindSecondLargestSort.js @@ -0,0 +1,22 @@ +/* + * Find Second Largest is a real technical interview question. + * Chances are you will be asked to find the second largest value + * inside of an array of numbers. You must also be able to filter + * out duplicate values. It's important to know how to do this with + * clean code that is also easy to explain. + * + * Resources: + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set + * + */ + +// Dummy data +const numberArray = [2,5,8,4,13,6,10,10,47,24,36,1]; + +const removeRepeatedNumbers = (array) => + [...new Set(array)] // -- A value in a Set() can only appear once + +//Implementation +const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a-b); +console.log(newSorted[newSorted.length - 2]); + From f328a50f46430f2303df4d2d552b2843ede7329a Mon Sep 17 00:00:00 2001 From: MacBookPro Date: Mon, 26 Oct 2020 20:35:31 -0500 Subject: [PATCH 2/9] Add FindSecondLargestSort --- Sorts/FindSecondLargestSort.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sorts/FindSecondLargestSort.js b/Sorts/FindSecondLargestSort.js index 0ec1432af1..6ec397947a 100644 --- a/Sorts/FindSecondLargestSort.js +++ b/Sorts/FindSecondLargestSort.js @@ -11,12 +11,12 @@ */ // Dummy data -const numberArray = [2,5,8,4,13,6,10,10,47,24,36,1]; +const numberArray = [2, 5, 8, 4, 13, 6, 10, 10, 47, 24, 36, 1]; const removeRepeatedNumbers = (array) => - [...new Set(array)] // -- A value in a Set() can only appear once + [...new Set(array)] // A value in a Set() can only appear once -//Implementation +// Implementation const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a-b); console.log(newSorted[newSorted.length - 2]); From 512767c79912136a8cfc0c79dad0bb94e80e769c Mon Sep 17 00:00:00 2001 From: MacBookPro Date: Mon, 26 Oct 2020 20:39:19 -0500 Subject: [PATCH 3/9] Add FindSecondLargestSort --- Sorts/FindSecondLargestSort.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Sorts/FindSecondLargestSort.js b/Sorts/FindSecondLargestSort.js index 6ec397947a..d51a7ec88d 100644 --- a/Sorts/FindSecondLargestSort.js +++ b/Sorts/FindSecondLargestSort.js @@ -1,22 +1,17 @@ /* - * Find Second Largest is a real technical interview question. - * Chances are you will be asked to find the second largest value - * inside of an array of numbers. You must also be able to filter - * out duplicate values. It's important to know how to do this with - * clean code that is also easy to explain. - * - * Resources: - * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set - * - */ - +* Find Second Largest is a real technical interview question. +* Chances are you will be asked to find the second largest value +* inside of an array of numbers. You must also be able to filter +* out duplicate values. It's important to know how to do this with +* clean code that is also easy to explain. +* +* Resources: +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set +*/ // Dummy data const numberArray = [2, 5, 8, 4, 13, 6, 10, 10, 47, 24, 36, 1]; - const removeRepeatedNumbers = (array) => [...new Set(array)] // A value in a Set() can only appear once - // Implementation const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a-b); -console.log(newSorted[newSorted.length - 2]); - +console.log(newSorted[newSorted.length - 2]); \ No newline at end of file From 4b9bca5e6955d1ac2b55158e379d1c952cad0064 Mon Sep 17 00:00:00 2001 From: MacBookPro Date: Mon, 26 Oct 2020 20:42:08 -0500 Subject: [PATCH 4/9] Add FindSecondLargestSort --- Sorts/FindSecondLargestSort.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sorts/FindSecondLargestSort.js b/Sorts/FindSecondLargestSort.js index d51a7ec88d..b90a7c33f9 100644 --- a/Sorts/FindSecondLargestSort.js +++ b/Sorts/FindSecondLargestSort.js @@ -1,17 +1,17 @@ -/* +/* * Find Second Largest is a real technical interview question. * Chances are you will be asked to find the second largest value * inside of an array of numbers. You must also be able to filter * out duplicate values. It's important to know how to do this with * clean code that is also easy to explain. -* -* Resources: +* +* Resources: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set */ // Dummy data -const numberArray = [2, 5, 8, 4, 13, 6, 10, 10, 47, 24, 36, 1]; +const numberArray = [2, 5, 8, 4, 13, 6, 10, 10, 47, 24, 36, 1] const removeRepeatedNumbers = (array) => [...new Set(array)] // A value in a Set() can only appear once // Implementation -const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a-b); -console.log(newSorted[newSorted.length - 2]); \ No newline at end of file +const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a - b) +console.log(newSorted[newSorted.length - 2]) \ No newline at end of file From 68791f22dfe592d857b8642f19a9124b6cb10dd4 Mon Sep 17 00:00:00 2001 From: MacBookPro Date: Mon, 26 Oct 2020 20:44:32 -0500 Subject: [PATCH 5/9] Add FindSecondLargestSort --- Sorts/FindSecondLargestSort.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sorts/FindSecondLargestSort.js b/Sorts/FindSecondLargestSort.js index b90a7c33f9..77ed051bd1 100644 --- a/Sorts/FindSecondLargestSort.js +++ b/Sorts/FindSecondLargestSort.js @@ -11,7 +11,8 @@ // Dummy data const numberArray = [2, 5, 8, 4, 13, 6, 10, 10, 47, 24, 36, 1] const removeRepeatedNumbers = (array) => - [...new Set(array)] // A value in a Set() can only appear once +[...new Set(array)] // A value in a Set() can only appear once + // Implementation const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a - b) -console.log(newSorted[newSorted.length - 2]) \ No newline at end of file +console.log(newSorted[newSorted.length - 2]) From 5814bc0badc0462bdda465a2cdb80ae6a2b8805e Mon Sep 17 00:00:00 2001 From: MacBookPro Date: Mon, 26 Oct 2020 20:46:21 -0500 Subject: [PATCH 6/9] Add FindSecondLargestSort --- Sorts/FindSecondLargestSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/FindSecondLargestSort.js b/Sorts/FindSecondLargestSort.js index 77ed051bd1..6b58a159fb 100644 --- a/Sorts/FindSecondLargestSort.js +++ b/Sorts/FindSecondLargestSort.js @@ -11,7 +11,7 @@ // Dummy data const numberArray = [2, 5, 8, 4, 13, 6, 10, 10, 47, 24, 36, 1] const removeRepeatedNumbers = (array) => -[...new Set(array)] // A value in a Set() can only appear once + [...new Set(array)] // A value in a Set() can only appear once // Implementation const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a - b) From dd8cbdc486004dde69bc32ae5cac14b927f2137b Mon Sep 17 00:00:00 2001 From: vinayak Date: Sat, 3 Apr 2021 14:22:17 +0530 Subject: [PATCH 7/9] Update and rename FindSecondLargestSort.js to FindSecondLargestElement.js --- ...estSort.js => FindSecondLargestElement.js} | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) rename Sorts/{FindSecondLargestSort.js => FindSecondLargestElement.js} (56%) diff --git a/Sorts/FindSecondLargestSort.js b/Sorts/FindSecondLargestElement.js similarity index 56% rename from Sorts/FindSecondLargestSort.js rename to Sorts/FindSecondLargestElement.js index 6b58a159fb..38462a8b3b 100644 --- a/Sorts/FindSecondLargestSort.js +++ b/Sorts/FindSecondLargestElement.js @@ -8,11 +8,20 @@ * Resources: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set */ -// Dummy data -const numberArray = [2, 5, 8, 4, 13, 6, 10, 10, 47, 24, 36, 1] -const removeRepeatedNumbers = (array) => - [...new Set(array)] // A value in a Set() can only appear once -// Implementation -const newSorted = removeRepeatedNumbers(numberArray).sort((a, b) => a - b) -console.log(newSorted[newSorted.length - 2]) +const secondLargestElement = (array) => { + const largestElement = Math.max(...array) + let element = 0 + + for (let i = 0; i < array.length; i++) { + if (element < array[i] && array[i] !== largestElement) { + element = array[i] + } + } + + return element +} + + + +export { secondLargestElement } From f5da7fa20c7f192dfcd9113be8311314e2a0049c Mon Sep 17 00:00:00 2001 From: vinayak Date: Sat, 3 Apr 2021 14:25:45 +0530 Subject: [PATCH 8/9] Update FindSecondLargestElement.js --- Sorts/FindSecondLargestElement.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sorts/FindSecondLargestElement.js b/Sorts/FindSecondLargestElement.js index 38462a8b3b..70ea02553e 100644 --- a/Sorts/FindSecondLargestElement.js +++ b/Sorts/FindSecondLargestElement.js @@ -22,6 +22,5 @@ const secondLargestElement = (array) => { return element } - - export { secondLargestElement } + From 0ae91bb2f03c73273143deb9b9927c9c7091f688 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sat, 3 Apr 2021 14:29:18 +0530 Subject: [PATCH 9/9] Update FindSecondLargestElement.js --- Sorts/FindSecondLargestElement.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Sorts/FindSecondLargestElement.js b/Sorts/FindSecondLargestElement.js index 70ea02553e..c87fc99a1e 100644 --- a/Sorts/FindSecondLargestElement.js +++ b/Sorts/FindSecondLargestElement.js @@ -23,4 +23,3 @@ const secondLargestElement = (array) => { } export { secondLargestElement } -