While trying to get all permutations using Heap's algorithm, I met trouble storing the results in array.
The result generated is (from console.log(arr);)
["1", "2", "3"]
["2", "1", "3"]
["3", "1", "2"]
["1", "3", "2"]
["2", "3", "1"]
["3", "2", "1"]
but only the last value is stored in the arr, and the array stored somehow is this (from console.log(JSON.stringify(allPermutations)); )
["3", "2", "1"]
["3", "2", "1"]
["3", "2", "1"]
["3", "2", "1"]
["3", "2", "1"]
["3", "2", "1"]
var allPermutations = [];
function swap(arr,index1,index2){
var dummy = arr[index1];
arr[index1] = arr[index2];
arr[index2] = dummy;
return arr;
}
function generate(n,arr){
if(n==1){
console.log(arr);
//result:
//["1", "2", "3"]
//["2", "1", "3"]
//["3", "1", "2"]
//["1", "3", "2"]
//["2", "3", "1"]
//["3", "2", "1"]
allPermutations.push(arr);
}else{
for(var i=0;i<n-1;i++){
generate(n-1,arr);
if( n%2 ==0){
arr = swap(arr,i,n-1);
}else{
arr = swap(arr,0,n-1);
}
}
generate(n - 1, arr);
}
}
generate(3,['1','2','3']);
console.log(JSON.stringify(allPermutations));
/*result:
["3","2","1"]
["3","2","1"]
["3","2","1"]
["3","2","1"]
["3","2","1"]
["3","2","1"]*/
What's wrong with this? Would love to understand. Thanks
Replace allPermutations.push(arr) with allPermutations.push(arr.slice()).
The problem is, you keep pushing the same array, then changing that array. When you push an array, you don't push a copy of it: you push a reference. There is only one array, and six references to it; when you read them out, they all read the same, because they are all the same array.
.slice() will give you a new array with the same elements; this way, you get six new arrays into your result, instead of six mentions of the same array.
From one of my earlier answers that is almost-but-not-quite a duplicate, a metaphor I like for this:
As a metaphor, imagine a theatre director in casting. He turns to an actor, says "You... you'll be Romeo.". Then he looks at the same actor, says "You... you'll be Mercutio. Here, Mercutio, take this sword. Romeo... who told you to get a sword?!?" completely failing to realise that, if Romeo and Mercutio are the same person, if one of them picks up a sword, the other does it too.
That's because arrays are objects, and objects are passed by value, but that value is a reference.
Then, your code keeps pushing the same arr reference to allPermutations. But the values in that reference are modified later.
Instead, you should push a copy of the array. You can copy it with .slice().
var allPermutations = [];
function swap(arr, index1, index2) {
var dummy = arr[index1];
arr[index1] = arr[index2];
arr[index2] = dummy;
return arr;
}
function generate(n, arr) {
if (n == 1) {
allPermutations.push(arr.slice());
} else {
for (var i = 0; i < n - 1; i++) {
generate(n - 1, arr);
if (n % 2 == 0) {
arr = swap(arr, i, n - 1);
} else {
arr = swap(arr, 0, n - 1);
}
}
generate(n - 1, arr);
}
}
generate(3, ['1', '2', '3']);
document.write(JSON.stringify(allPermutations));
Related
I have an array:
["1", "2","3","4","5"]
that I would like to change to:
[["1, 0 ,0 ,0"], ["2, 0, 0, 0"],["3,0,0,0"],["4,0,0,0"],["5,0,0,0"]]
I've tried to achieve this with the following code:
var arr1 = ["1", "2","3","4","5"];
var arr2 = [,"0", "0","0"];
for(var z=0; z<arr1.length; z++)
{
arr1[z] = arr1[z].concat(arr2);
console.log(arr1)
}
However, this doesn't achieve what I want and places commas between each item, which I think is because it is not a string? I've tried using .join too but I couldn't get that to work either.
Could someone point me in the right direction for this please? Thanks.
You first need to convert each element to array and then append the second array to the newly converted array element. The following code snippet would make it clear -
var arr1 = ["1", "2", "3", "4", "5"];
var arr2 = [ "0", "0", "0"];
for (var z = 0; z < arr1.length; z++) {
// First convert the element into a list
arr1[z] = [arr1[z]];
// Then append the second array to it
arr1[z] = arr1[z].concat(arr2);
// Another way -
//arr1[z].push(...arr2);
}
// Final array -
console.log(arr1);
Using Map() :
Another way to do it would be using map() as below -
var arr1 = ["1", "2", "3", "4", "5"];
var arr2 = [ "0", "0", "0"];
arr1 = arr1.map(el=>[el,...arr2]);
console.log(arr1);
Using ForEach()
You could also do it using forEach() method -
var arr1 = ["1", "2", "3", "4", "5"];
var arr2 = [ "0", "0", "0"];
// this will note mutate original arr1 and store result in res
let res = [];
// ... is the spread operator which will allows iterable objects to be expanded in place
arr1.forEach(el=>res.push([el,...arr2]));
// final result
console.log(res);
There's probably numerous other ways as well... These were just few I have listed. You could even do it using naive nested loops method if you don't know anything about map(), forEach(), or concat() / push() methods.
Hope this helps !
Please try the following example
const array = ["1", "2", "3", "4", "5"];
const output = array.map((entry) => [`${entry}, 0, 0, 0`]);
console.log(output);
See
Array.prototype.map()
Template literals (Template strings)
if it's set in stone, you can do
let arr = []
var arr1 = ["1", "2","3","4","5"];
var arr2 = ["0", "0","0"];
arr1.forEach(el=>arr.push([el, ...arr2]));
console.log(arr)
Use map to go through each of your element and append the array with zeros, transform it to a string and wrap in an array again which will hold one value
const a = ["1", "2", "3", "4", "5"]
const arr = [0, 0, 0, 0];
const res = a.map(x => [
[x, ...arr].join()
])
console.log(res)
I'm looking for an efficient solution to sort an array depending of how many times an element appears
For example :
let values = ["10", "4", "4", "4", "7", "7"]
I think the best output would be something like [{number, frequency}, ...], which, in the example would look like this :
[{4, 3}, {7, 2}, {10, 1}]
I've seen a lot of ways to do it, but every solution just sort the array depending of the frequency, without any access on how many times the element appears.
At the moment I only have this code that I got from another StackOverflow topic (can't remember which one sorry)
var map = values.reduce(function(p, c) {
p[c] = (p[c] || 0) + 1;
return p;
}, {});
var newTypesArray = Object.keys(map).sort(function(a, b) {
return map[a] < map[b];
});
console.log(newTypesArray);
It's doing a great job at sorting depending on the frequency, but I can't access how many times an element is repeated. And I have no idea how to do it...
Any thoughts?
const arr = [1, 1, 1, 2, 2, 3];
// use reduce for that.
const result = arr.reduce((result, item) => {
const count = result[item];
if (count === undefined) {
result[item] = 1;
} else {
result[item] += 1;
}
return result;
}, {});
// You'll get the result similar to this: {[item]: [count]}
// And then you can transform it into entries array:
const entries = Object.entries(result);
// entries are [[item, count], ...];
// And then sort
const sorted = entries.sort((entryA, entryB) => entryA[1] - entryB[1]);
// You'll have ascending sorted array by count.
console.log(sorted);
Something like this might do the trick:
const data = ["10", "7", "5", "4", "4", "7", "7", "4", "5", "4"];
const result = data.reduce((r, c) => (r[c] = (r[c] || 0) + 1, r), {});
var keys=Object.keys(result).sort((a,b)=>result[b]-result[a]); // sorted keys
console.log(keys.map(k=>k+':'+result[k])); // output, ordered by keys
It is up to you how you want to return the results. result contains the count and keys the actual values.
You can use Object.entries() over the object you got from reduce now sorting the array of key-value pairs and finally mapping to the array of {number, frequency}:
let values = ["10", "4", "4", "4", "7", "7"]
var map = Object.entries(values.reduce(function(p, c) {
p[c] = (p[c] || 0) + 1;
return p;
}, {}))
.sort((a, b) => a[1] < b[1])
.map((a) => ({[a[0]]:a[1]}));
console.log(map);
I think the best output would be something like [{number, frequency}, ...]
Actually these are not valid JavaScript objects inside your output array, if you want to show them as pairs you can use an array of arrays instead of this, the result will be like this:
[[number, frequency], ...]
In your code you were almost there, but you just need to map the result of your first reduce call to get the desired pair:
let values = ["10", "4", "4", "4", "7", "7"];
var map = values.reduce(function(p, c) {
p[c] = (p[c] || 0) + 1;
return p;
}, {});
var newTypesArray = Object.keys(map).map(function(k) {
return [+k, map[k]];
}).sort((a,b) => b[1] - a[1]);
console.log(newTypesArray);
I am merging two sorted arrays in JavaScript. When I call function with two arrays having numbers it works fine, but when I call that function with strings then it does not work. Why?
function mergeSortedArrays(array1, array2) {
const mergedArray = [];
let array1Item = array1[0];
let array2Item = array2[0];
let i = 1;
let j = 1;
if (array1.length === 0) {
return array2;
}
if (array2.length === 0) {
return array1;
}
while (array1Item || array2Item) {
if (array2Item === undefined || array1Item < array2Item) {
mergedArray.push(array1Item);
array1Item = array1[i];
i++;
} else {
mergedArray.push(array2Item);
array2Item = array2[j];
j++;
}
}
console.log(mergedArray);
}
//working?
mergeSortedArrays([0, 3, 4, 12, 222], [3, 4, 6, 30]);
// not working why?
mergeSortedArrays(["0", "3", "4", "12", "222"], ["3", "4", "6", "30"]);
As said in the comments, strings in JS are compared lexically , so, "222" is smaller than "3".
A solution that I see that you can use, is this one:
After checking the arrays for nullity, then concat then into mergedArray, then use the JS function sort(), with the basic return of value1 - value2, that way it will sort the strings in the order you want, and also will work for numbers.
(Further read: Why is one string greater than the other when comparing strings in JavaScript?)
function mergeSortedArrays(array1, array2) {
let mergedArray = [];
if (array1.length === 0) {
return array2;
}
if (array2.length === 0) {
return array1;
}
mergedArray = array1.concat(array2)
mergedArray.sort(function(a, b) {
return a - b
})
console.log(mergedArray);
return mergedArray;
}
mergeSortedArrays([0, 3, 4, 12, 222], [3, 4, 6, 30]);
mergeSortedArrays(["0", "3", "4", "12", "222"], ["3", "4", "6", "30"]);
BUT, be knowing that this solution will only work as expected if the strings are representations of numbers ("1", "2",...), if it is something like "aa", "abc", "b" it probably won't work well and another solution may be needed. (something like this: https://stackoverflow.com/a/51169/8732818)
Sort for string works differently than numbers. Its based on the ASCII table values. For example "99" > "100000" return should return true
I got this:
var arr= ["three", "2", "five", "ten", "111", 1, 2, "forty", "33", 33];
I need it to be 2 new arrays
One with just the numbers, and the other with just the strings
Like this:
var strArr = ["three", "2", "five", "ten", "111","forty", "33"];
var numArr = [1, 2, 33];
How should I do it?
You can use typeof operator in combination with filter method which accepts as parameter a callback function.
The filter() method creates a new array with all elements that pass
the test implemented by the provided function.
var arr= ["three", "2", "five", "ten", "111", 1, 2, "forty", "33", 33];
var numbers = arr.filter(function(item){
return typeof item == 'number';
});
var strings = arr.filter(function(item){
return typeof item == 'string';
});
console.log(numbers);
console.log(strings);
You can use arrow functions for a light solution.
['number', 'string'].map(i => arr.filter(a => typeof a == i));
var arr= ["three", "2", "five", "ten", "111", 1, 2, "forty", "33", 33];
var num=[];
var str=[];
for(i=0;i<arr.length;i++){
if(arr[i].includes('"') || arr[i].includes("'"))
str.push(arr[i]);
else
num.push(arr[i]);
}
You need to use create a function that checks if what you have given is a string or an integer and push the value to the right array.
I have not tested the code below but it should be a good starting place.
var arr= ["three", "2", "five", "ten", "111", 1, 2, "forty", "33", 33];
var strArr = [];
var numArr = [];
var arrayLength = arr.length();
// iterates trough the array
for (i; i < arr.length(); i++)
{
//Check if the given value in the array is string
if (typeof arr[i] === 'string')
{
// adds the value you are checking to the strArr
strArr.push(arr[i]);
}
//Check if the given value in the array is integer
if (typeof arr[i] === 'number' && isFinite(value))
{
// adds the value you are checking to the numArr
numArr.push(arr[i]);
}
}
Hope that helps
I am trying to sort an array which looks like this:
var arr = ["a", "c", "1", "e", "-", "4", "p", "$", "~", "6", "33", "h"];
the order I want to get should be:
var arr = ["a", "c", "e", "h", "p", "-", "~", "$", "1", "33", "4", "6"];
I already have a working solution but I am not sure if this is the best (fastest) way how to do it.
arr.sort(function(a, b) {
return a.localeCompare(b);
});
for (var i = 0; i < arrLength; i++) {
if (arr[i].localeCompare("a") >= 0) {
if (i > 0) {
Array.prototype.push.apply(arr, arr.splice(0, i));
}
break;
}
}
Yes, you should include the logic in the comparison function. This will be easier to understand than first sorting by one criteria and then shifting around parts of the array by some other criteria, and it will be reusable in other situations.
You want
arr.sort(function(a, b) {
var aIsAlphabetical = a.localeCompare("a") >= 0,
bIsAlphabetical = b.localeCompare("a") >= 0;
if (!aIsAlphabetical && bIsAlphabetical)
return 1;
if (aIsAlphabetical && !bIsAlphabetical)
return -1;
return a.localeCompare(b);
});
or, shortened:
arr.sort(function(a, b) {
return (b.localeCompare("a")>=0)-(a.localeCompare("a")>=0) || a.localeCompare(b);
});
If you're looking for fastest code, you should set up a benchmark at the targetted system with a good example array.