Skip to content

Commit

Permalink
Add day 18
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 14, 2019
1 parent 417a56a commit 9ecfd1b
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -34,6 +34,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
15. [Day 15 -- Pascal's Triangle](./day15) -- [http://codetoexpress.tech/dc/day15/](http://codetoexpress.tech/dc/day15/)
16. [Day 16 -- Tower of Hanoi](./day16) -- [http://codetoexpress.tech/dc/day16/](http://codetoexpress.tech/dc/day16/)
17. [Day 17 -- N Queens Problem](./day17) -- [http://codetoexpress.tech/dc/day17/](http://codetoexpress.tech/dc/day17/)
18. [Day 18 -- Frequency Count and Check Power N](./day18) -- [http://codetoexpress.tech/dc/day18/](http://codetoexpress.tech/dc/day18/)

## [More Problems](./BONUS/README.md)

Expand Down
35 changes: 35 additions & 0 deletions day18/JavaScript/checkPowerN_madhav.js
@@ -0,0 +1,35 @@
/**
* @author MadhavBahlMD
* @date 14/01/2019
* Using frequency counter
*/

function checkPowerN (arr1, arr2, num) {
if (arr1.length !== arr2.length) return false;

let freq1 = {};
freq2 = {};

// Make frequency counter for array 1
let powElement;
for (let element of arr1) {
powElement = Math.pow (element, num);
freq1[powElement] = (freq1[powElement] || 0) + 1;
}

// Make frequency counter for array 2
for (let element of arr2)
freq2[element] = (freq2[element] || 0) + 1;

// Compare the objects
for (let key in freq1) {
if (!(key in freq2))
return false;
if (freq1[key] !== freq2[key])
return false;
}
return true;
}

console.log (checkPowerN ([1, 2, 3, 4], [4, 9, 1, 16], 2));
console.log (checkPowerN ([3, 4, 5, 2], [1, 2, 3], 4));
25 changes: 25 additions & 0 deletions day18/JavaScript/checkPowerN_madhav2.js
@@ -0,0 +1,25 @@
/**
* @author MadhavBahlMD
* @date 14/01/2019
* Using Brute Force Search
*/

function checkPowerN (arr1, arr2, num) {
if (arr1.length !== arr2.length) return false;

for (let i=0; i<arr1.length; i++)
arr1[i] = Math.pow (arr1[i], num);

for (let element of arr1) {
let pos = arr2.indexOf (element);

if (pos < 0)
return false;

arr2.splice (pos, 1);
}
return true;
}

console.log (checkPowerN ([1, 2, 3, 4], [4, 9, 1, 16], 2));
console.log (checkPowerN ([3, 4, 5, 2], [1, 2, 3], 4));
24 changes: 24 additions & 0 deletions day18/JavaScript/countUniques1.js
@@ -0,0 +1,24 @@
/**
* @author MadhavBahlMD
* @date 14/01/2019
* Count uniques using multiple pointers (since the input array is sorted)
*/


function countUniques (arr) {
if (arr.length === 0) return 0;

let i=0;

for (let j=1; j<arr.length; j++) {
if (arr[i] !== arr[j]) {
i++;
arr[i] = arr[j];
}
}

return i+1;
}

console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
console.log (`Number of unique elements = ${countUniques([])}`);
23 changes: 23 additions & 0 deletions day18/JavaScript/countUniques2.js
@@ -0,0 +1,23 @@
/**
* @author MadhavBahlMD
* @date 14/01/2019
* Count Uniques using frequency object
*/

function countUniques (arr) {
let freq = {},
count = 0;

for (let element of arr) {
if (!(element in freq)) {
count++;
freq[element] = 1;
} else
freq[element]++;
}

return count;
}

console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
console.log (`Number of unique elements = ${countUniques([])}`);
27 changes: 27 additions & 0 deletions day18/JavaScript/countUniques3.js
@@ -0,0 +1,27 @@
/**
* @author MadhavBahlMD
* @date 14/01/2019
* Count Uniques using brute force search
*/

function countUniques (arr) {
if (arr.length === 0) return 0;

let count = 1; // first element is always unique, since there is nothing behind it
for (let j=1; j<arr.length; j++) {
let flag = true;
for (let i=0; i<j; i++) {
if (arr[i] === arr[j]) {
flag = false;
break;
}
}
if (flag)
count++;
}

return count;
}

console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
console.log (`Number of unique elements = ${countUniques([])}`);
24 changes: 24 additions & 0 deletions day18/JavaScript/freqCounter_madhav.js
@@ -0,0 +1,24 @@
/**
* @author MadhavBahlMD
* @date 01/14/2018
* Frequency Ccounter using object
*/

function freqCounter (arr) {
let freq = {};

// Iterate over the array and update frequency object
for (let element of arr) {
freq[element] = (freq[element] || 0) + 1;
}

// Print the output
printFrequency (freq);
}

function printFrequency (freqObj) {
for (let key in freqObj)
console.log (`'${key}' is present ${freqObj[key]} time(s)`);
}

freqCounter ([ 1, 2, 3, 1, 3, 4, 4, 4, 4, 2, 5]);

0 comments on commit 9ecfd1b

Please sign in to comment.