Sorting automobiles by multiple criteria - javascript

I have a function that is supposed to be able to compare automobiles based on year, type, make etc, but I'm having trouble getting it to work.
I define my automobiles like this:
function Automobile( year, make, model, type ){
this.year = year; //integer (ex. 2001, 1995)
this.make = make; //string (ex. Honda, Ford)
this.model = model; //string (ex. Accord, Focus)
this.type = type; //string (ex. Pickup, SUV)
}
var automobiles = [
new Automobile(1995, "Honda", "Accord", "Sedan"),
new Automobile(1990, "Ford", "F-150", "Pickup"),
new Automobile(2000, "GMC", "Tahoe", "SUV"),
new Automobile(2010, "Toyota", "Tacoma", "Pickup"),
new Automobile(2005, "Lotus", "Elise", "Roadster"),
new Automobile(2008, "Subaru", "Outback", "Wagon")
];
The above part has no issues, but my sorting function has some problems, and I can't figure out what they are. Running this code gives me the error:
Uncaught SyntaxError: Unexpected end of input
/*This function sorts arrays using an arbitrary comparator. You pass it a comparator and an array of objects appropriate for that comparator and it will return a new array which is sorted with the largest object in index 0 and the smallest in the last index*/
function sortArr( comparator, array ){
var sorted = array;
var min; var temp;
for(var i = 0; i < array.length-1; i++){
min = i;
for(var j = i+1; j < array.length; j++){
var comp = comparator(array[j], array[min]);
if(comp)
min = j;
}
if(min != i){
temp = sorted[i];
sorted[i] = sorted[min];
sorted[min] = temp;
}
}
return sorted;
}
function exComparator( int1, int2){
if (int1 > int2){
return true;
} else {
return false;
}
}
/*For all comparators if cars are 'tied' according to the comparison rules then the order of those 'tied' cars is not specified and either can come first*/
/*This compares two automobiles based on their year. Newer cars are "greater" than older cars.*/
function yearComparator( auto1, auto2){
return exComparator(auto1.make, auto2.make);
}
/*This compares two automobiles based on their make. It should be case insensitive and makes which are alphabetically earlier in the alphabet are "greater" than ones that come later.*/
function makeComparator( auto1, auto2){
return exComparator(auto1.make, auto2.make);
}
/*This compares two automobiles based on their type. The ordering from "greatest" to "least" is as follows: roadster, pickup, suv, wagon, (types not otherwise listed). It should be case insensitive. If two cars are of equal type then the newest one by model year should be considered "greater".*/
function typeComparator( auto1, auto2){
var auto1_type = switch(auto1.type.toLowerCase()){
case("roadster"): return 5;
case("pickup"): return 4;
case("suv"): return 3;
case("wagon"): return 2;
case("sedan"): return 1;
}
var auto2_type = switch(auto2.type.toLowerCase()){
case("roadster"): return 5;
case("pickup"): return 4;
case("suv"): return 3;
case("wagon"): return 2;
case("sedan"): return 1;
}
if(auto1_type > auto2_type) {
return auto1.type;
}
else if(auto2_type > auto1_type) {
return auto2.type;
}
else {
if(auto1.year > auto2.year) {
return auto1.type;
}
else {
return auto2.type;
}
}
}
function printArr(array){
for(var i = 0; i < array.length; i++){
var car = array[i];
console.log(car.year + ' ' + car.make + ' ' + car.model + ' ' + car.type);
}
}

Just some examples for sorting callbacks without switch, but with an hash table instead.
function Automobile(year, make, model, type) {
this.year = year; //integer (ex. 2001, 1995)
this.make = make; //string (ex. Honda, Ford)
this.model = model; //string (ex. Accord, Focus)
this.type = type; //string (ex. Pickup, SUV)
}
function yearComparator(a, b) {
return a.year - b.year;
}
function makeComparator(a, b) {
return a.make.localeCompare(b.make);
}
function modelComparator(a, b) {
return a.model.localeCompare(b.model);
}
function typeComparator(a, b) {
var types = { roadster: 5, pickup: 4, suv: 3, wagon: 2, sedan: 1 },
aa = types[a.type.toLowerCase()] || 0,
bb = types[b.type.toLowerCase()] || 0;
return aa - bb;
}
var automobiles = [new Automobile(1995, "Honda", "Accord", "Sedan"), new Automobile(1990, "Ford", "F-150", "Pickup"), new Automobile(2000, "GMC", "Tahoe", "SUV"), new Automobile(2010, "Toyota", "Tacoma", "Pickup"), new Automobile(2005, "Lotus", "Elise", "Roadster"), new Automobile(2008, "Subaru", "Outback", "Wagon")];
automobiles.sort(yearComparator);
document.write('<pre>sorted by year: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
automobiles.sort(makeComparator);
document.write('<pre>sorted by make: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
automobiles.sort(modelComparator);
document.write('<pre>sorted by model: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
automobiles.sort(typeComparator);
document.write('<pre>sorted by type: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
// bonus chained sort callbacks, get sorted by type DESC and year DESC
automobiles.sort(function (a, b) {
return -typeComparator(a, b) || -yearComparator(a, b);
});
document.write('<pre>sorted by type DESC and year DESC: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');

You forget a curly brace { in the if statement
if (comp) <-- here
min = j;
}
Also you have wrong switch statement, you are trying assign the value from return statement
var auto1_type =
switch (auto1.type.toLowerCase()) {
case "roadster":
return 5;
...
Your switch statement should look like this
var auto1_type;
switch (auto1.type.toLowerCase()) {
case "roadster":
auto1_type = 5;
case ....
return auto1_type;

Related

Javascript includes and map together [duplicate]

I am supposed to write a program in JavaScript to find all the anagrams within a series of words provided. e.g.:
monk, konm, nkom, bbc, cbb, dell, ledl, llde
The output should be categorised into rows:
1. monk konm, nkom;
2. bbc cbb;
3. dell ledl, llde;
I already sorted them into alphabetical order and put them into an array. i.e.:
kmno kmno bbc bbc dell dell
However I am stuck in comparing and finding the matching anagram within the array.
Any help will be greatly appreciated.
Javascript objects are excellent for this purpose, since they are essentially key/value stores:
// Words to match
var words = ["dell", "ledl", "abc", "cba"];
// The output object
var anagrams = {};
for (var i in words) {
var word = words[i];
// sort the word like you've already described
var sorted = sortWord(word);
// If the key already exists, we just push
// the new word on the the array
if (anagrams[sorted] != null) {
anagrams[sorted].push(word);
}
// Otherwise we create an array with the word
// and insert it into the object
else {
anagrams[sorted] = [ word ];
}
}
// Output result
for (var sorted in anagrams) {
var words = anagrams[sorted];
var sep = ",";
var out = "";
for (var n in words) {
out += sep + words[n];
sep = "";
}
document.writeln(sorted + ": " + out + "<br />");
}
Here is my take:
var input = "monk, konm, bbc, cbb, dell, ledl";
var words = input.split(", ");
for (var i = 0; i < words.length; i++) {
var word = words[i];
var alphabetical = word.split("").sort().join("");
for (var j = 0; j < words.length; j++) {
if (i === j) {
continue;
}
var other = words[j];
if (alphabetical === other.split("").sort().join("")) {
console.log(word + " - " + other + " (" + i + ", " + j + ")");
}
}
}
where the output would be (the word, the match and the index of both):
monk - konm (0, 1)
konm - monk (1, 0)
bbc - cbb (2, 3)
cbb - bbc (3, 2)
dell - ledl (4, 5)
ledl - dell (5, 4)
To get the characters in the in alphabetical order, I used split("") ot get an array, called sort() and used join("") to get a string from the array.
Simple Solution
function anagrams(stringA, stringB) {
return cleanString(stringA) === cleanString(stringB);
}
function cleanString(str) {
return str.replace(/[^\w]/g).toLowerCase().split('').sort().join()
}
anagrams('monk','konm')
If it is anagrams function will return true otherwise false
I worked through a similar question to this today and wanted to share the results of my work. I was focused on just detecting the anagram so processing the list of words was not part of my exercise but this algorithm should provide a highly performant way to detect an anagram between two words.
function anagram(s1, s2){
if (s1.length !== s2.length) {
// not the same length, can't be anagram
return false;
}
if (s1 === s2) {
// same string must be anagram
return true;
}
var c = '',
i = 0,
limit = s1.length,
match = 0,
idx;
while(i < s1.length){
// chomp the next character
c = s1.substr(i++, 1);
// find it in the second string
idx = s2.indexOf(c);
if (idx > -1) {
// found it, add to the match
match++;
// assign the second string to remove the character we just matched
s2 = s2.substr(0, idx) + s2.substr(idx + 1);
} else {
// not found, not the same
return false;
}
}
return match === s1.length;
}
I think technically is can be solved like this:
function anagram(s1, s2){
return s1.split("").sort().join("") === s2.split("").sort().join("");
}
The reason I chose the earlier approach is that it is more performant for larger strings since you don't need to sort either string, convert to an array or loop through the entire string if any possible failure case is detected.
Probably not the most efficient way, but a clear way around using es6
function sortStrChars(str) {
if (!str) {
return;
}
str = str.split('');
str = str.sort();
str = str.join('');
return str;
}
const words = ["dell", "ledl", "abc", "cba", 'boo'];
function getGroupedAnagrams(words) {
const anagrams = {}; // {abc:[abc,cba], dell:[dell, ledl]}
words.forEach((word) => {
const sortedWord = sortStrChars(word);
if (anagrams[sortedWord]) {
return anagrams[sortedWord].push(word);
}
anagrams[sortedWord] = [word];
});
return anagrams;
}
const groupedAnagrams = getGroupedAnagrams(words);
for (const sortedWord in groupedAnagrams) {
console.log(groupedAnagrams[sortedWord].toString());
}
I had this question in an interview. Given an array of words ['cat', 'dog', 'tac', 'god', 'act'], return an array with all the anagrams grouped together. Makes sure the anagrams are unique.
var arr = ['cat', 'dog', 'tac', 'god', 'act'];
var allAnagrams = function(arr) {
var anagrams = {};
arr.forEach(function(str) {
var recurse = function(ana, str) {
if (str === '')
anagrams[ana] = 1;
for (var i = 0; i < str.length; i++)
recurse(ana + str[i], str.slice(0, i) + str.slice(i + 1));
};
recurse('', str);
});
return Object.keys(anagrams);
}
console.log(allAnagrams(arr));
//["cat", "cta", "act", "atc", "tca", "tac", "dog", "dgo", "odg", "ogd", "gdo", "god"]
Best and simple way to solve is using for loops and traversing it to each string and then store their result in object.
Here is the solution :-
function anagram(str1, str2) {
if (str1.length !== str2.length) {
return false;
}
const result = {};
for (let i=0;i<str1.length;i++) {
let char = str1[i];
result[char] = result[char] ? result[char] += 1 : result[char] = 1;
}
for (let i=0;i<str2.length;i++) {
let char = str2[i];
if (!result[char]) {
return false;
}
else {
result[char] = -1;
}
}
return true;
}
console.log(anagram('ronak','konar'));
I know this is an ancient post...but I just recently got nailed during an interview on this one. So, here is my 'new & improved' answer:
var AnagramStringMiningExample = function () {
/* Author: Dennis Baughn
* This has also been posted at:
* http://stackoverflow.com/questions/909449/anagrams-finder-in-javascript/5642437#5642437
* Free, private members of the closure and anonymous, innner function
* We will be building a hashtable for anagrams found, with the key
* being the alphabetical char sort (see sortCharArray())
* that the anagrams all have in common.
*/
var dHash = {};
var sortCharArray = function(word) {
return word.split("").sort().join("");
};
/* End free, private members for the closure and anonymous, innner function */
/* This goes through the dictionary entries.
* finds the anagrams (if any) for each word,
* and then populates them in the hashtable.
* Everything strictly local gets de-allocated
* so as not to pollute the closure with 'junk DNA'.
*/
(function() {
/* 'dictionary' referring to English dictionary entries. For a real
* English language dictionary, we could be looking at 20,000+ words, so
* an array instead of a string would be needed.
*/
var dictionaryEntries = "buddy,pan,nap,toot,toto,anestri,asterin,eranist,nastier,ratines,resiant,restain,retains,retinas,retsina,sainter,stainer,starnie,stearin";
/* This could probably be refactored better.
* It creates the actual hashtable entries. */
var populateDictionaryHash = function(keyword, newWord) {
var anagrams = dHash[keyword];
if (anagrams && anagrams.indexOf(newWord) < 0)
dHash[keyword] = (anagrams+','+newWord);
else dHash[keyword] = newWord;
};
var words = dictionaryEntries.split(",");
/* Old School answer, brute force
for (var i = words.length - 1; i >= 0; i--) {
var firstWord = words[i];
var sortedFirst = sortCharArray(firstWord);
for (var k = words.length - 1; k >= 0; k--) {
var secondWord = words[k];
if (i === k) continue;
var sortedSecond = sortCharArray(secondWord);
if (sortedFirst === sortedSecond)
populateDictionaryHash(sortedFirst, secondWord);
}
}/*
/*Better Method for JS, using JS Array.reduce(callback) with scope binding on callback function */
words.reduce(function (prev, cur, index, array) {
var sortedFirst = this.sortCharArray(prev);
var sortedSecond = this.sortCharArray(cur);
if (sortedFirst === sortedSecond) {
var anagrams = this.dHash[sortedFirst];
if (anagrams && anagrams.indexOf(cur) < 0)
this.dHash[sortedFirst] = (anagrams + ',' + cur);
else
this.dHash[sortedFirst] = prev + ','+ cur;
}
return cur;
}.bind(this));
}());
/* return in a nice, tightly-scoped closure the actual function
* to search for any anagrams for searchword provided in args and render results.
*/
return function(searchWord) {
var keyToSearch = sortCharArray(searchWord);
document.writeln('<p>');
if (dHash.hasOwnProperty(keyToSearch)) {
var anagrams = dHash[keyToSearch];
document.writeln(searchWord + ' is part of a collection of '+anagrams.split(',').length+' anagrams: ' + anagrams+'.');
} else document.writeln(searchWord + ' does not have anagrams.');
document.writeln('<\/p>');
};
};
Here is how it executes:
var checkForAnagrams = new AnagramStringMiningExample();
checkForAnagrams('toot');
checkForAnagrams('pan');
checkForAnagrams('retinas');
checkForAnagrams('buddy');
Here is the output of the above:
toot is part of a collection of 2
anagrams: toto,toot.
pan is part of a collection of 2
anagrams: nap,pan.
retinas is part of a collection of 14
anagrams:
stearin,anestri,asterin,eranist,nastier,ratines,resiant,restain,retains,retinas,retsina,sainter,stainer,starnie.
buddy does not have anagrams.
My solution to this old post:
// Words to match
var words = ["dell", "ledl", "abc", "cba"],
map = {};
//Normalize all the words
var normalizedWords = words.map( function( word ){
return word.split('').sort().join('');
});
//Create a map: normalizedWord -> real word(s)
normalizedWords.forEach( function ( normalizedWord, index){
map[normalizedWord] = map[normalizedWord] || [];
map[normalizedWord].push( words[index] );
});
//All entries in the map with an array with size > 1 are anagrams
Object.keys( map ).forEach( function( normalizedWord , index ){
var combinations = map[normalizedWord];
if( combinations.length > 1 ){
console.log( index + ". " + combinations.join(' ') );
}
});
Basically I normalize every word by sorting its characters so stackoverflow would be acefkloorstvw, build a map between normalized words and the original words, determine which normalized word has more than 1 word attached to it -> That's an anagram.
Maybe this?
function anagram (array) {
var organized = {};
for (var i = 0; i < array.length; i++) {
var word = array[i].split('').sort().join('');
if (!organized.hasOwnProperty(word)) {
organized[word] = [];
}
organized[word].push(array[i]);
}
return organized;
}
anagram(['kmno', 'okmn', 'omkn', 'dell', 'ledl', 'ok', 'ko']) // Example
It'd return something like
{
dell: ['dell', 'ledl'],
kmno: ['kmno', okmn', 'omkn'],
ko: ['ok', ko']
}
It's a simple version of what you wanted and certainly it could be improved avoiding duplicates for example.
My two cents.
This approach uses XOR on each character in both words. If the result is 0, then you have an anagram. This solution assumes case sensitivity.
let first = ['Sower', 'dad', 'drown', 'elbow']
let second = ['Swore', 'add', 'down', 'below']
// XOR all characters in both words
function isAnagram(first, second) {
// Word lengths must be equal for anagram to exist
if (first.length !== second.length) {
return false
}
let a = first.charCodeAt(0) ^ second.charCodeAt(0)
for (let i = 1; i < first.length; i++) {
a ^= first.charCodeAt(i) ^ second.charCodeAt(i)
}
// If a is 0 then both words have exact matching characters
return a ? false : true
}
// Check each pair of words for anagram match
for (let i = 0; i < first.length; i++) {
if (isAnagram(first[i], second[i])) {
console.log(`'${first[i]}' and '${second[i]}' are anagrams`)
} else {
console.log(`'${first[i]}' and '${second[i]}' are NOT anagrams`)
}
}
function isAnagram(str1, str2) {
var str1 = str1.toLowerCase();
var str2 = str2.toLowerCase();
if (str1 === str2)
return true;
var dict = {};
for(var i = 0; i < str1.length; i++) {
if (dict[str1[i]])
dict[str1[i]] = dict[str1[i]] + 1;
else
dict[str1[i]] = 1;
}
for(var j = 0; j < str2.length; j++) {
if (dict[str2[j]])
dict[str2[j]] = dict[str2[j]] - 1;
else
dict[str2[j]] = 1;
}
for (var key in dict) {
if (dict[key] !== 0)
return false;
}
return true;
}
console.log(isAnagram("hello", "olleh"));
I have an easy example
function isAnagram(strFirst, strSecond) {
if(strFirst.length != strSecond.length)
return false;
var tempString1 = strFirst.toLowerCase();
var tempString2 = strSecond.toLowerCase();
var matched = true ;
var cnt = 0;
while(tempString1.length){
if(tempString2.length < 1)
break;
if(tempString2.indexOf(tempString1[cnt]) > -1 )
tempString2 = tempString2.replace(tempString1[cnt],'');
else
return false;
cnt++;
}
return matched ;
}
Calling function will be isAnagram("Army",Mary);
Function will return true or false
let words = ["dell", "ledl","del", "abc", "cba", 'boo'];
//sort each item
function sortArray(data){
var r=data.split('').sort().join().replace(/,/g,'');
return r;
}
var groupObject={};
words.forEach((item)=>{
let sorteditem=sortArray(item);
//Check current item is in the groupObject or not.
//If not then add it as an array
//else push it to the object property
if(groupObject[sorteditem])
return groupObject[sorteditem].push(item);
groupObject[sorteditem]=[sorteditem];
});
//to print the result
for(i=0;i<Object.keys(groupObject).length;i++)
document.write(groupObject[Object.keys(groupObject)[i]] + "<br>");
/* groupObject value:
abc: (2) ["abc", "cba"]
boo: ["boo"]
del: ["del"]
dell: (2) ["dell", "ledl"]
OUTPUT:
------
dell,ledl
del
abc,cba
boo
*/
Compare string length, if not equal, return false
Create character Hashmap which stores count of character in strA e.g. Hello --> {H: 1, e: 1, l: 2, o: 1}
Loop over the second string and lookup the current character in Hashmap. If not exist, return false, else decrement the value by 1
If none of the above return falsy, it must be an anagram
Time complexity: O(n)
function isAnagram(strA: string, strB: string): boolean {
const strALength = strA.length;
const strBLength = strB.length;
const charMap = new Map<string, number>();
if (strALength !== strBLength) {
return false;
}
for (let i = 0; i < strALength; i += 1) {
const current = strA[i];
charMap.set(current, (charMap.get(current) || 0) + 1);
}
for (let i = 0; i < strBLength; i += 1) {
const current = strB[i];
if (!charMap.get(current)) {
return false;
}
charMap.set(current, charMap.get(current) - 1);
}
return true;
}
function findAnagram(str1, str2) {
let mappedstr1 = {}, mappedstr2 = {};
for (let item of str1) {
mappedstr1[item] = (mappedstr1[item] || 0) + 1;
}
for (let item2 of str2) {
mappedstr2[item2] = (mappedstr2[item2] || 0) + 1;
}
for (let key in mappedstr1) {
if (!mappedstr2[key]) {
return false;
}
if (mappedstr1[key] !== mappedstr2[key]) {
return false;
}
}
return true;
}
console.log(findAnagram("hello", "hlleo"));
Another example only for comparing 2 strings for an anagram.
function anagram(str1, str2) {
if (str1.length !== str2.length) {
return false;
} else {
if (
str1.toLowerCase().split("").sort().join("") ===
str2.toLowerCase().split("").sort().join("")
) {
return "Anagram";
} else {
return "Not Anagram";
}
}
}
console.log(anagram("hello", "olleh"));
console.log(anagram("ronak", "konar"));
const str1 ="1123451"
const str2 = "2341151"
function anagram(str1,str2) {
let count = 0;
if (str1.length!==str2.length) { return false;}
for(i1=0;i1<str1.length; i1++) {
for (i2=0;i2<str2.length; i2++) {
if (str1[i1]===str2[i2]){
count++;
break;
}
}
}
if (count===str1.length) { return true}
}
anagram(str1,str2)
Another solution for isAnagram using reduce
const checkAnagram = (orig, test) => {
return orig.length === test.length
&& orig.split('').reduce(
(acc, item) => {
let index = acc.indexOf(item);
if (index >= 0) {
acc.splice(index, 1);
return acc;
}
throw new Error('Not an anagram');
},
test.split('')
).length === 0;
};
const isAnagram = (tester, orig, test) => {
try {
return tester(orig, test);
} catch (e) {
return false;
}
}
console.log(isAnagram(checkAnagram, '867443', '473846'));
console.log(isAnagram(checkAnagram, '867443', '473846'));
console.log(isAnagram(checkAnagram, '867443', '475846'));
var check=true;
var str="cleartrip";
var str1="tripclear";
if(str.length!=str1.length){
console.log("Not an anagram");
check=false;
}
console.log(str.split("").sort());
console.log("----------"+str.split("").sort().join(''));
if(check){
if((str.split("").sort().join(''))===((str1.split("").sort().join('')))){
console.log("Anagram")
}
else{
console.log("not a anagram");
}
}
Here is my solution which addresses a test case where the input strings which are not anagrams, can be removed from the output. Hence the output contains only the anagram strings. Hope this is helpful.
/**
* Anagram Finder
* #params {array} wordArray
* #return {object}
*/
function filterAnagram(wordArray) {
let outHash = {};
for ([index, word] of wordArray.entries()) {
let w = word.split("").sort().join("");
outHash[w] = !outHash[w] ? [word] : outHash[w].concat(word);
}
let filteredObject = Object.keys(outHash).reduce(function(r, e) {
if (Object.values(outHash).filter(v => v.length > 1).includes(outHash[e])) r[e] = outHash[e]
return r;
}, {});
return filteredObject;
}
console.log(filterAnagram(['monk', 'yzx','konm', 'aaa', 'ledl', 'bbc', 'cbb', 'dell', 'onkm']));
i have recently faced this in the coding interview, here is my solution.
function group_anagrams(arr) {
let sortedArr = arr.map(item => item.split('').sort().join(''));
let setArr = new Set(sortedArr);
let reducedObj = {};
for (let setItem of setArr) {
let indexArr = sortedArr.reduce((acc, cur, index) => {
if (setItem === cur) {
acc.push(index);
}
return acc;
}, []);
reducedObj[setItem] = indexArr;
}
let finalArr = [];
for (let reduceItem in reducedObj) {
finalArr.push(reducedObj[reduceItem].map(item => arr[item]));
}
return finalArr;
}
group_anagrams(['car','cra','rca', 'cheese','ab','ba']);
output will be like
[
["car", "cra", "rca"],
["cheese"],
["ab", "ba"]
]
My solution has more code, but it avoids using .sort(), so I think this solution has less time complexity. Instead it makes a hash out of every word and compares the hashes:
const wordToHash = word => {
const hash = {};
// Make all lower case and remove spaces
[...word.toLowerCase().replace(/ /g, '')].forEach(letter => hash[letter] ? hash[letter] += 1 : hash[letter] = 1);
return hash;
}
const hashesEqual = (obj1, obj2) => {
const keys1 = Object.keys(obj1), keys2 = Object.keys(obj2);
let match = true;
if(keys1.length !== keys2.length) return false;
for(const key in keys1) { if(obj1[key] !== obj2[key]) match = false; break; }
return match;
}
const checkAnagrams = (word1, word2) => {
const hash1 = wordToHash(word1), hash2 = wordToHash(word2);
return hashesEqual(hash1, hash2);
}
console.log( checkAnagrams("Dormitory", "Dirty room") );
/*This is good option since
logic is easy,
deals with duplicate data,
Code to check anagram in an array,
shows results in appropriate manner,
function check can be separately used for comparing string in this regards with all benefits mentioned above.
*/
var words = ["deuoll", "ellduo", "abc","dcr","frt", "bu","cba","aadl","bca","elduo","bac","acb","ub","eldou","ellduo","ert","tre"];
var counter=1;
var ele=[];
function check(str1,str2)
{
if(str2=="")
return false;
if(str1.length!=str2.length)
return false;
var r1=[...(new Set (str1.split('').sort()))];
var r2=[...(new Set (str2.split('').sort()))];
var flag=true;
r1.forEach((item,index)=>
{
if(r2.indexOf(item)!=index)
{ flag=false;}
});
return flag;
}
var anagram=function ()
{
for(var i=0;i<words.length && counter!=words.length ;i++)
{
if(words[i]!="")
{
document.write("<br>"+words[i]+":");
counter++;
}
for(var j=i+1;j<words.length && counter !=words.length+1;j++)
{
if(check(words[i],words[j]))
{
ele=words[j];
document.write(words[j]+"&nbsp");
words[j]="";
counter++;
}
}
}
}
anagram();
If you just need count of anagrams
const removeDuplicatesAndSort = [...new Set(yourString.split(', '))].map(word => word.split('').sort().join())
const numberOfAnagrams = removeDuplicatesAndSort.length - [...new Set(removeDuplicatesAndSort)].length
function isAnagram(str1, str2){
let count = 0;
if (str1.length !== str2.length) {
return false;
} else {
let val1 = str1.toLowerCase().split("").sort();
let val2 = str2.toLowerCase().split("").sort();
for (let i = 0; i < val2.length; i++) {
if (val1[i] === val2[i]) {
count++;
}
}
if (count == str1.length) {
return true;
}
}
return false;
}
console.log(isAnagram("cristian", "Cristina"))
function findAnagrams (str, arr){
let newStr = "";
let output = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
for (let k = 0; k < str.length; k++) {
if (str[k] === arr[i][j] && str.length === arr[i].length) {
newStr += arr[i][j];
}
}
} if(newStr.length === str.length){
output.push(newStr);
newStr = "";
}
}
return output;
}
const getAnagrams = (...args) => {
const anagrams = {};
args.forEach((arg) => {
const letters = arg.split("").sort().join("");
if (anagrams[letters]) {
anagrams[letters].push(arg);
} else {
anagrams[letters] = [arg];
}
});
return Object.values(anagrams);
}
function isAnagaram(str1, str2){
if(str1.length!== str2.length){
return false;
}
var obj1 = {};
var obj2 = {};
for(var arg of str1){
obj1[arg] = (obj1[arg] || 0 ) + 1 ;
}
for(var arg of str2){
obj2[arg] = (obj2[arg] || 0 ) + 1 ;
}
for( var key in obj1){
if(obj1[key] !== obj2[key]){
return false;
}
}
return true;
}
console.log(isAnagaram('texttwisttime' , 'timetwisttext'));
let validAnagram = (firstString, secondString) => {
if (firstString.length !== secondString.length) {
return false;
}
let secondStringArr = secondString.split('');
for (var char of firstString) {
charIndexInSecondString = secondString.indexOf(char);
if (charIndexInSecondString === -1) {
return false;
}
secondString = secondString.replace(char, '');
}
return true;
}

Store count of integers in order using javascript

I have string like the following:
11222233344444445666
What I would like to do is output the number followed the times it was displayed:
112433475163
Question is, I want this to be efficient. I can store this in an object as the following:
1: { id: 1, displayed: 2},
2: { id: 2, displayed: 1},
3: { id: 3, displayed: 2},
etc.
I can access this object and increment displayed.
My issues is, there is no guarantee in the order. I would like to store the keys in the order they are in the string. How do I accomplish the importance of the order in the object?
This is a proposal for run length coding with an array which holds infomation about one charcter and the count of it:
{
"char": "1",
"count": 2
},
var string = "11222233344444445666",
array = function () {
var r = [], o = {};
string.split('').forEach(function (a, i, aa) {
if (a !== aa[i - 1]) {
o[a] = { char: a, count: 0 };
r.push(o[a]);
}
o[a].count++;
});
return r;
}(string);
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
Quick solution with for loop:
var str = "7771122229933344444445666",
obj = {},
len = str.length,
val = null,
count_str = "",
key = "";
for (var i = 0; i < len; i++) {
val = str[i], key = 'k' + val;
if (!obj[key]) {
obj[key] = {'id': val, 'displayed': 1};
} else {
obj[key].displayed++;
}
}
for (var p in obj) {
count_str += obj[p]['id'] + obj[p]['displayed'];
}
console.log(count_str); // "7312249233475163"
because you have such a small set of distinct numbers, I seen no reason why you can't use a array (yeah it's not super ideal memorywise if you skip values and it becomes sparse, but for such a small subset it won't affect you enough to worry of it). Then you can use (number-1) as the index and increment that number as needed.
var counts = [];
var str = "11222233344444445666";
for(var i in str){
var index = parseInt(str[i])-1
counts[index] = (counts[index]||0)+1;
}
for(var i in counts){
var which = 1+parseInt(i);
var count = counts[i];
console.log("# of " + which +"'s: "+count);
}
https://jsfiddle.net/ga0fqpqn/
note: You shouldn't need the parseInt(i)... just +i should work but I think jsfiddle has a bug with it about it defaulting i to handle like a string.
You could store an additional array with the order of the numbers, which you only append to if the object doesn't yet contain the given number. Then once you're done counting, iterate through that array and output the number and the count from the lookup dictionary.
var chars = "1234576123452345".split("");
var order = [];
var hash = {};
chars.forEach(function(char) {
if (!hash[char]) {
hash[char] = 1;
order.push(char);
} else {
hash[char]++;
}
});
console.log(order.map(function(char) {
return char + hash[char];
}).join(""));
// "12233343537161"

How to add two List in Javascript

This is a question from an interview book. I'm using Javascript to build the list, but the principle is still the same with List from lisp or clojure or any language. Any answer using any kind of language is accepted.
Write a function that adds the two numbers and returns the sum as a linked list. If the sum is greater than 10, extra one digit will be carried to the tail
EXAMPLE
Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
My List Class always start with one head, then followed with next, next, next and so on
List = function(){
this.head = {
value: null,
next: null
}
}
EXAMPLE of List:
var listA = new List();
/* sample data-structure
listA = {
head:{
value: 3,
next:{
value: 1,
next: {
value: 5,
next: null
}
}
}
}
*/
This is a proposal of a possible solution for different list length.
Contains
Node object with the properties value and next.
Function setValues, which creates a new list of nodes with the given array.
Function add which takes two lists of nodes and returns a new list of nodes with the result of summing the values of the same level.
function Node(value) {
this.value = value;
this.next = undefined;
}
function setValues(a) {
return a.reduceRight(function (r, v) {
var o = new Node(v);
if (r) {
o.next = r;
}
return o;
}, undefined);
}
function add(l1, l2) {
var value = 0,
last, list;
while (l1 || l2) {
if (l1) {
value += l1.value;
l1 = l1.next;
}
if (l2) {
value += l2.value;
l2 = l2.next;
}
if (last) {
last.next = new Node(value % 10);
last = last.next;
} else {
list = new Node(value % 10);
last = list;
}
value = value / 10 | 0;
}
if (value) {
last.next = new Node(value);
}
return list;
}
var list1 = setValues([3, 1, 5]),
list2 = setValues([5, 9, 2]),
list3 = add(list1, list2);
document.write('<pre>' + JSON.stringify(list1, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(list2, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(list3, 0, 4) + '</pre>');
Bonus: Version with recursive style.
function Node(value) {
this.value = value;
this.next = undefined;
}
function setValues(a) {
var n = new Node(a.shift());
if (a.length) {
n.next = setValues(a);
}
return n;
}
function add(l1, l2, r) {
function v(o, p) { return o && o[p] || 0; }
r = (r || 0) + v(l1, 'value') + v(l2, 'value');
var n = new Node(r % 10);
l1 = v(l1, 'next');
l2 = v(l2, 'next');
r = r / 10 | 0;
if (l1 || l2 || r) {
n.next = add(l1, l2, r);
}
return n;
}
var list1 = setValues([3, 1, 5]),
list2 = setValues([5, 9, 2]),
list3 = add(list1, list2);
document.write('<pre>' + JSON.stringify(list1, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(list2, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(list3, 0, 4) + '</pre>');

Fastest way to double the length of an array in JavaScript?

[1,2,3,4,5].duplicate(); // [1,2,3,4,5,1,2,3,4,5]
Maybe something like:
var array = [1,2,3,4,5];
array.push(array);
But what's the fastest method?
You can use concat and replace the original array with the new one:
array = array.concat(array);
Or push with apply (modifies the original array):
[].push.apply(array, array);
Or push with the spread operator (modifies the original array):
array.push(...array);
The FASTEST way to do something in JS is usually using simple C-like native statements.
I think this will be the fastest one:
function duplicate(arr) {
for(var i=0,len=arr.length;i<len;i++)
arr[i+len]=arr[i];
}
A slower but more elegant one would be this:
arr=arr.concat(arr);
Or this one:
[].push.apply(arr,arr);
EcmaScript 6 also allows you to do the same using the spread operator. This operator just puts all the values of the array for you in the place you have written it, so this var arr = [0,1,2,3];console.log(...arr) turns into var arr = [0,1,2,3];console.log(0,1,2,3).
arr.push(...arr);
However, EcmaScript 6 is not yet widely supported (and this feature neither) so if I were you I wouldn't use it. Although ES 6 has just been approved & released (as #LyeFish said in the comments) most of browsers are still working on it.
EcmaScript-262 6th edition officially released 5 days ago (thanks to #LyeFish)!
http://www.ecma-international.org/news/index.html
According to my test results, the winners are concat followed by push methods. I also tested looping method and my own newly-introduced splice routine.
Here's the testing "infrastructure" code that was used:
var toArray = Function.prototype.call.bind(Array.prototype.slice);
Function.prototype.testerTimes = function(number) {
var func = this;
return function() {
var args = toArray(arguments), i = 0;
for( ; i < number; func.apply(this, args), i++);
};
};
Function.prototype.testerTime = function(units) {
var func = this;
return function() {
var start = Date.now(), diff;
func.apply(this, toArray(arguments));
diff = Date.now() - start;
return units === "s" ? diff / 1000 : units === "m" ? diff / 60000 : units === "h" ? diff / 3600000 : diff;
};
};
Function.prototype.testerToConsole = function(prefix, message) {
var func = this;
return function() {
console.log(prefix + message + func.apply(this, toArray(arguments)));
};
};
Function.prototype.makeTestReady = function(times, units, prefix, message) {
return this.testerTimes(times).testerTime(units).testerToConsole(prefix, message);
};
Here's the test code:
function genArray(num) {
for(var i = 0, arr = []; i < num; arr.push(++i));
return arr;
};
var numberOfRuns = 1000000;
var timeUnit = "s";
var messagePrefix = " ";
var funcs = [
function duplicateConcat(arr) {
var arrCopy = arr.slice(0);
return arrCopy.concat(arrCopy);
},
function duplicatePush(arr) {
var arrCopy = arr.slice(0);
arrCopy.push.apply(arrCopy, arrCopy);
return arrCopy;
},
function duplicateLoop(arr) {
var arrCopy = arr.slice(0);
for(var i = 0, len = arrCopy.length; i < len; i++) {
arrCopy[len + i] = arrCopy[i];
}
return arrCopy;
},
function duplicateSplice(arr) {
var arrCopy = arr.slice(0);
arrCopy.splice.apply(arrCopy, [arrCopy.length, 0].concat(arrCopy));
return arrCopy;
}
].map(function(func, index, arr) {
return func.makeTestReady(numberOfRuns, timeUnit, messagePrefix, func.name + ": ");
});
for(var i = 5; i < 25; i+= 5) {
console.log(i + "-element array:");
funcs.forEach(function(func) {
func(genArray(i));
});
}
And, here are the results of 1,000,000 runs of each function for arrays of sizes 5, 10, 15, and 20:
5-element array:
duplicateConcat: 0.236
duplicatePush: 0.228
duplicateLoop: 0.372
duplicateSplice: 0.45
10-element array:
duplicateConcat: 0.241
duplicatePush: 0.273
duplicateLoop: 0.433
duplicateSplice: 0.48
15-element array:
duplicateConcat: 0.261
duplicatePush: 0.293
duplicateLoop: 0.5
duplicateSplice: 0.522
20-element array:
duplicateConcat: 0.24
duplicatePush: 0.311
duplicateLoop: 0.602
duplicateSplice: 0.558

How do I sort a concatenated array in JavaScript?

My code:
var company=new Array("Kestrel Moon:","BB:");
var basicPri=new Array(1165,1231);
for(var i=0;i<15;i++){
var companyTotal=company[i].concat(basicPri[…
document.write(""+companyTotal+"")
It shows on the screen:
Kestrel Moon: 1165
BB: 1231
I want to sort the array so that it goes ascending order of highest value of price so it should display it as:
BB: 1231
Kestrel Moon: 1165
A normal sort would not do it as it would sort the prices but the company names stay where they are, how do I sort both arrays so it would display what I want to display?
Thank You
typically, you would group the name with number:
function sortNumber(a,b)
{
return b[1] - a[1]; // comparing using 2nd element
}
var n = [["ccc", 10.23], ["www", 23.12], ["mmm", 0.56]];
document.write(n.sort(sortNumber));
output:
www,23.12,ccc,10.23,mmm,0.56
and if you use jQuery, you can create a table from the result:
function sortNumber(a,b)
{
return b[1] - a[1]; // comparing using 2nd element
}
var n = [["ccc", 10.23], ["www", 23.12], ["mmm", 0.56]];
sorted = n.sort(sortNumber);
$('body').append('<table id="thetable"></table>');
$('#thetable').css({borderCollapse: 'collapse'})
for (i = 0; i < sorted.length; i++) {
$('#thetable').append('<tr><td>' + sorted[i][0] + '</td><td>' + sorted[i][1] + '</td></tr>')
}
$('td').css({ border: '1px solid #ccc', padding: '0.2em 1em' });
Here's a possibility using your code as base (i.e. 2 arrays):
// comparaison functor
function sorter(a, b) {
return a.name.localeCompare(b.name);
}
// build a sorted array of object like
// [ { "name": "foo", price: 123 }, ... ]
// sorted by company name (Locale aware order)
function mysort(comp, prices) {
var res = [];
for (var i = 0; i < comp.length; i++) {
res.push({"name": comp[i], "price": prices[i]});
}
return res.sort(sorter);
}
var company = ["Kestrel Moon:", "BB:"];
var basicPri = [1165, 1231];
var companyTotal = "";
var arr = mysort(company, basicPri);
for (var i in arr) {
companyTotal += arr[i].name + " " + arr[i].price + "<br/>";
}
document.write(companyTotal);
Tested on chrome 6.0.427.0 dev
A normal sort would not do it as it would sort the prices but the company names stay where they are ...
In this case, an array of objects would probably be a better data structure for your data. Consider the example below:
var dict = [
{company: 'Kestrel Moon:', basicPri: '1165'},
{company: 'BB:', basicPri: '1231'}
];
var sorted = dict.sort(function(a, b) {
return a.company.localeCompare(b.company);
});
console.log(sorted[0].company + ' ' + sorted[0].basicPri);
console.log(sorted[1].company + ' ' + sorted[1].basicPri);
// Prints:
// ------------------
// BB: 1231
// Kestrel Moon: 1165

Categories