-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
417a56a
commit 9ecfd1b
Showing
10 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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([])}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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([])}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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([])}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); |
Oops, something went wrong.