diff --git a/src/algorithms/Fequency Counter based/Anagrams.js b/src/algorithms/Fequency Counter based/Anagrams.js new file mode 100644 index 00000000..30f138f5 --- /dev/null +++ b/src/algorithms/Fequency Counter based/Anagrams.js @@ -0,0 +1,45 @@ +//program to check if two hard-coded strings are anagrams or not. + +function isAnagram(str1, str2) { + +//firstly, they need to be of the same length. + if (str1.length !== str2.length) { + return false; + } + +//frequency counters + var frequencyCounter1 = {} + , frequencyCounter2 = {}; + +//value of frequency counter being updated for every string character. +/*what this actually does is, it creates an object, in which the key value pairs are as follows --> + + key : value :: character : frequency + +*/ + +//added, toLowerCase() method to make it case insensitive. + for (let val of str1.toLowerCase()) { + frequencyCounter1[val] = (frequencyCounter1[val] || 0) + 1; + } + + for (let val of str2.toLowerCase()) { + frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1; + } + + for (let key in frequencyCounter1) { + //checks if the particular key exists or not + if (!(key in frequencyCounter2)) { + return false; + } + + //then checks, if for the particular keys of the two objects, the values are equal or not. + if (frequencyCounter2[key] !== frequencyCounter1[key]) { + return false; + } + } + return true; +} + +//Remember Shutter Island :P +isAnagram("Rachel Solando", "Dolores Chanal"); diff --git a/src/algorithms/Fequency Counter based/areEqualNoOfSquares.js b/src/algorithms/Fequency Counter based/areEqualNoOfSquares.js new file mode 100644 index 00000000..b196f433 --- /dev/null +++ b/src/algorithms/Fequency Counter based/areEqualNoOfSquares.js @@ -0,0 +1,40 @@ +//Program to check if the elements in the second array, are the squares of each and every element of the first array. +function areEqualNoOfSqs(arr1, arr2) { + +//firstly, they need to be of the same length. + + if (arr1.length !== arr2.length) { + return false; + } + +//frequency counters + var freqCounter1 = {} + , freqCounter2 = {}; + +//value of frequency counter being updated for every string character. +/*what this actually does is, it creates an object, in which the key value pairs are as follows --> + + key : value :: character : frequency + +*/ + for (let val of arr1) { + freqCounter1[val] = (freqCounter1[val] || 0) + 1; + } + + for (let val of arr2) { + freqCounter2[val] = (freqCounter2[val] || 0) + 1; + } + + for (let key in freqCounter1) { + if (!(key ** 2 in freqCounter2)) { + return false; + } + + if (freqCounter2[key ** 2] !== freqCounter1[key]) { + return false; + } + } + return true; +} + +areEqualNoOfSqs([2, 3, 4, 1, 2, 5], [25, 4, 9, 1, 4, 16]); diff --git a/src/algorithms/Fequency Counter based/areThereDuplicates_FQ.js b/src/algorithms/Fequency Counter based/areThereDuplicates_FQ.js new file mode 100644 index 00000000..48206b28 --- /dev/null +++ b/src/algorithms/Fequency Counter based/areThereDuplicates_FQ.js @@ -0,0 +1,17 @@ + +//Program to check if duplicate elements are present inside an array. + +function areThereDuplicatesFQ(){ + let obj = {}; + + for(let val in arguments){ + obj[arguments[val]] = (obj[arguments[val]]||0)+1; + } + + for(let key in obj){ + if(obj[key]>1) return true; + } + return false; +} + +areThereDuplicatesFQ(1,2,5,7,8); diff --git a/src/algorithms/Fequency Counter based/sameFrequency.js b/src/algorithms/Fequency Counter based/sameFrequency.js new file mode 100644 index 00000000..9c644d9b --- /dev/null +++ b/src/algorithms/Fequency Counter based/sameFrequency.js @@ -0,0 +1,37 @@ +//Numeric anagrams detector + +function sameFrequency(strA, strB) { + +//convert the array, into a string + var str1 = strA.toString() + , str2 = strB.toString(); + + if (str1.length !== str2.length) { + return false; + } + + var freqCounter1 = {} + , freqCounter2 = {}; + + + for (let val of str1) { + freqCounter1[val] = (freqCounter1[val] || 0) + 1; + } + + for (let val of str2) { + freqCounter2[val] = (freqCounter2[val] || 0) + 1; + } + + for (let key in freqCounter1) { + if (!(key in freqCounter2)) { + return false; + } + + if (freqCounter2[key] !== freqCounter1[key]) { + return false; + } + } + return true; +} + +sameFrequency(552181, 152581);