From a4e68c86fcfabea9da53707f2c83e6fb3ba0ea0d Mon Sep 17 00:00:00 2001 From: Peter Rabbit Date: Thu, 12 Mar 2020 19:45:13 -0700 Subject: [PATCH 1/3] used Array.sort to avoid overflow issue used Array.sort to avoid overflow issue and create a long array with many empty elements --- js/distinct.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/js/distinct.js b/js/distinct.js index eda4a99..6743360 100644 --- a/js/distinct.js +++ b/js/distinct.js @@ -7,19 +7,17 @@ */ function solution(A) { - - var seen = []; - var count = 0; - var len = A.length; - for (var i = 0; i < len; i++) { - var item = A[i]; - if (seen[item] !== 1) { - seen[item] = 1; - count++ + let sorted = [], count = 0; + + sorted = A.sort((a, b) => a - b); + + for(let i = 0; i < sorted.length; i++) { + if (sorted[i] !== sorted[i-1]) { + count++; } } + return count; - } @@ -40,4 +38,4 @@ test([2, 1, 1, 2, 3, 1]); function test(...params) { console.log('\n(', ...params, ')\n'); console.log('\n=>', solution(...params), '\n\n'); -} \ No newline at end of file +} From 988cb7b265db940a3e36d9036b852994f6129327 Mon Sep 17 00:00:00 2001 From: Peter Rabbit Date: Thu, 12 Mar 2020 19:49:58 -0700 Subject: [PATCH 2/3] used match to avoid overflow issue used math to avoid overflow issue e.g; let temp = [9, 2, 3, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; in this case, `17 * Number(A.reverse().toString().replace(/,/g, ''));` returns NaN --- js/decimal-representation.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/js/decimal-representation.js b/js/decimal-representation.js index 27d7e9b..b3c9fc8 100644 --- a/js/decimal-representation.js +++ b/js/decimal-representation.js @@ -15,15 +15,19 @@ console.log('// DECIMAL REPRESENTATION //'); function solution(A) { - - let newNumber = 17 * Number(A.reverse().toString().replace(/,/g, '')); - - let total = 0; - newNumber.toString().split('').map(e => total += Number(e)); - return total; + let sum = 0, overflow = 0, result = 0; + + for (let i = 0; i < A.length; i ++) { + sum = 17 * A[i] + overflow; + overflow = parseInt(sum / 10); + result += parseInt(sum % 10); + } + + result += parseInt(overflow % 10) + parseInt(overflow / 10); + + return result; } - test([3, 5, 1]); // 9 @@ -32,4 +36,4 @@ test([3, 5, 1]); function test(...params) { console.log('\n(', ...params, ')\n'); console.log('\n=>', solution(...params), '\n\n'); -} \ No newline at end of file +} From df97767b495938809465a45eca2b4fc845db89a2 Mon Sep 17 00:00:00 2001 From: Peter Rabbit Date: Fri, 24 Apr 2020 17:16:34 -0700 Subject: [PATCH 3/3] Update cyclic-rotation.js --- js/cyclic-rotation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/cyclic-rotation.js b/js/cyclic-rotation.js index 13c7dfd..19acb86 100644 --- a/js/cyclic-rotation.js +++ b/js/cyclic-rotation.js @@ -10,7 +10,7 @@ console.log('// CYCLIC ROTATION //'); function solution(A, K) { - K = (A.length > K) ? K : K % A.length; + K = K % A.length; var d = A.slice(0, A.length - K); var e = A.splice(A.length - K); @@ -33,4 +33,4 @@ test([5, -1000], 1); function test(...params) { console.log('\n(', ...params, ')\n'); console.log('\n=>', solution(...params), '\n\n'); -} \ No newline at end of file +}