From d4d91d41f488a8517e8094e6fc5872ee80bb032a Mon Sep 17 00:00:00 2001 From: Kapil Raghuwanshi Date: Tue, 1 Jun 2021 13:47:42 +0530 Subject: [PATCH 1/2] Added javascript solution to this program --- .../10.08 - Find Duplicates/findDuplicates.js | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/chapter10/10.08 - Find Duplicates/findDuplicates.js b/chapter10/10.08 - Find Duplicates/findDuplicates.js index 07bfb1c..fd7da09 100644 --- a/chapter10/10.08 - Find Duplicates/findDuplicates.js +++ b/chapter10/10.08 - Find Duplicates/findDuplicates.js @@ -1,2 +1,46 @@ -// sort numbers -// stream through numbers and print elements which is a duplicate of the immediate previous number +// We're given 4 kilobytes (4 * 2^10 * 8 = 32*2^10 bits) of main memory(RAM) to write a program to print all duplicate entries in an array +// from 1 to N where N is at most 32,000 (32*2^10). + +// We can use an Int8Array that uses 1 Byte per element to store 0's if we haven't seen it and 1's if we have seen it. +// The Int8Array typed array represents an array of twos-complement 8-bit signed integers. +// 4 KB > 32,000 bits + +// Let's have a JavaScript Solution of this program: + +function FindDuplicates(arr, range = 32000) { + // Initialize integer 8 bit array with 0's at every index + const numbersSeen = new Int8Array(range); + const duplicates = []; + for (let i = 0; i < arr.length; i++) { + if (numbersSeen[arr[i]] === 0) { + numbersSeen[arr[i]] = 1; + } else { + duplicates.push(arr[i]); + } + } + return duplicates; +} + + const arr = [ + 0, + 1, + 1, + 2, + 4, + 6, + 7, + 9, + 9, + 34, + 56, + 78, + 90, + 101, + 345, + 789, + 999, + 999, + 11000 + ]; + FindDuplicates(arr); +// [1, 9, 999] From 9b3f0a4f777c3a1d6904306befc6a11649f0ccf4 Mon Sep 17 00:00:00 2001 From: Kapil Raghuwanshi Date: Tue, 31 Aug 2021 17:46:35 +0530 Subject: [PATCH 2/2] Added Time and Space complexity for the program. --- chapter10/10.08 - Find Duplicates/findDuplicates.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/chapter10/10.08 - Find Duplicates/findDuplicates.js b/chapter10/10.08 - Find Duplicates/findDuplicates.js index fd7da09..1562e64 100644 --- a/chapter10/10.08 - Find Duplicates/findDuplicates.js +++ b/chapter10/10.08 - Find Duplicates/findDuplicates.js @@ -5,6 +5,9 @@ // The Int8Array typed array represents an array of twos-complement 8-bit signed integers. // 4 KB > 32,000 bits +// Time Complexity - O(n) +// Space Complexity - O(n) + // Let's have a JavaScript Solution of this program: function FindDuplicates(arr, range = 32000) {