Skip to content

Commit 9ecfd1b

Browse files
committed
Add day 18
1 parent 417a56a commit 9ecfd1b

File tree

10 files changed

+419
-0
lines changed

10 files changed

+419
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
3434
15. [Day 15 -- Pascal's Triangle](./day15) -- [http://codetoexpress.tech/dc/day15/](http://codetoexpress.tech/dc/day15/)
3535
16. [Day 16 -- Tower of Hanoi](./day16) -- [http://codetoexpress.tech/dc/day16/](http://codetoexpress.tech/dc/day16/)
3636
17. [Day 17 -- N Queens Problem](./day17) -- [http://codetoexpress.tech/dc/day17/](http://codetoexpress.tech/dc/day17/)
37+
18. [Day 18 -- Frequency Count and Check Power N](./day18) -- [http://codetoexpress.tech/dc/day18/](http://codetoexpress.tech/dc/day18/)
3738

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 14/01/2019
4+
* Using frequency counter
5+
*/
6+
7+
function checkPowerN (arr1, arr2, num) {
8+
if (arr1.length !== arr2.length) return false;
9+
10+
let freq1 = {};
11+
freq2 = {};
12+
13+
// Make frequency counter for array 1
14+
let powElement;
15+
for (let element of arr1) {
16+
powElement = Math.pow (element, num);
17+
freq1[powElement] = (freq1[powElement] || 0) + 1;
18+
}
19+
20+
// Make frequency counter for array 2
21+
for (let element of arr2)
22+
freq2[element] = (freq2[element] || 0) + 1;
23+
24+
// Compare the objects
25+
for (let key in freq1) {
26+
if (!(key in freq2))
27+
return false;
28+
if (freq1[key] !== freq2[key])
29+
return false;
30+
}
31+
return true;
32+
}
33+
34+
console.log (checkPowerN ([1, 2, 3, 4], [4, 9, 1, 16], 2));
35+
console.log (checkPowerN ([3, 4, 5, 2], [1, 2, 3], 4));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 14/01/2019
4+
* Using Brute Force Search
5+
*/
6+
7+
function checkPowerN (arr1, arr2, num) {
8+
if (arr1.length !== arr2.length) return false;
9+
10+
for (let i=0; i<arr1.length; i++)
11+
arr1[i] = Math.pow (arr1[i], num);
12+
13+
for (let element of arr1) {
14+
let pos = arr2.indexOf (element);
15+
16+
if (pos < 0)
17+
return false;
18+
19+
arr2.splice (pos, 1);
20+
}
21+
return true;
22+
}
23+
24+
console.log (checkPowerN ([1, 2, 3, 4], [4, 9, 1, 16], 2));
25+
console.log (checkPowerN ([3, 4, 5, 2], [1, 2, 3], 4));

day18/JavaScript/countUniques1.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 14/01/2019
4+
* Count uniques using multiple pointers (since the input array is sorted)
5+
*/
6+
7+
8+
function countUniques (arr) {
9+
if (arr.length === 0) return 0;
10+
11+
let i=0;
12+
13+
for (let j=1; j<arr.length; j++) {
14+
if (arr[i] !== arr[j]) {
15+
i++;
16+
arr[i] = arr[j];
17+
}
18+
}
19+
20+
return i+1;
21+
}
22+
23+
console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
24+
console.log (`Number of unique elements = ${countUniques([])}`);

day18/JavaScript/countUniques2.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 14/01/2019
4+
* Count Uniques using frequency object
5+
*/
6+
7+
function countUniques (arr) {
8+
let freq = {},
9+
count = 0;
10+
11+
for (let element of arr) {
12+
if (!(element in freq)) {
13+
count++;
14+
freq[element] = 1;
15+
} else
16+
freq[element]++;
17+
}
18+
19+
return count;
20+
}
21+
22+
console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
23+
console.log (`Number of unique elements = ${countUniques([])}`);

day18/JavaScript/countUniques3.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 14/01/2019
4+
* Count Uniques using brute force search
5+
*/
6+
7+
function countUniques (arr) {
8+
if (arr.length === 0) return 0;
9+
10+
let count = 1; // first element is always unique, since there is nothing behind it
11+
for (let j=1; j<arr.length; j++) {
12+
let flag = true;
13+
for (let i=0; i<j; i++) {
14+
if (arr[i] === arr[j]) {
15+
flag = false;
16+
break;
17+
}
18+
}
19+
if (flag)
20+
count++;
21+
}
22+
23+
return count;
24+
}
25+
26+
console.log (`Number of unique elements = ${countUniques([1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7])}`);
27+
console.log (`Number of unique elements = ${countUniques([])}`);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 01/14/2018
4+
* Frequency Ccounter using object
5+
*/
6+
7+
function freqCounter (arr) {
8+
let freq = {};
9+
10+
// Iterate over the array and update frequency object
11+
for (let element of arr) {
12+
freq[element] = (freq[element] || 0) + 1;
13+
}
14+
15+
// Print the output
16+
printFrequency (freq);
17+
}
18+
19+
function printFrequency (freqObj) {
20+
for (let key in freqObj)
21+
console.log (`'${key}' is present ${freqObj[key]} time(s)`);
22+
}
23+
24+
freqCounter ([ 1, 2, 3, 1, 3, 4, 4, 4, 4, 2, 5]);

0 commit comments

Comments
 (0)