This question already has answers here:
Find ith permutation in javascript
(3 answers)
Closed 4 years ago.
Despite reading a lot of Q/A about permutation/combination: Finding All Combinations of JavaScript array values + JavaScript - Generating combinations from n arrays with m elements I have not found the right way to get the kind of result I'm looking for.
I got a 10 values array:
var arr = [0,1,2,3,4,5,6,7,8,9];
If I'm right, the number of all possible permuted arrays of unique values (no duplicates):
[5,9,1,8,2,6,7,0,4,3] [4,8,0,2,1,9,7,3,6,5] ...
is 2x3x4x5x6x7x8x9x10 = 3628800
I'm trying to produce a function to dynamically create the 'n' array. For example:
function createArray(0) -> [0,1,2,3,4,5,6,7,8,9]
function createArray(45648) -> [0,1,5,3,2,8,7,9,6] (something like...)
function createArray(3628800) -> [9,8,7,6,5,4,3,2,1,0]
The way I'm figuring to achieve it is:
createArray(1) permutes the 2 last signs (8,9 -> 9,8)
createArray(2->6) permutes the 3 last signs (8,7,9 -> 9,8,7)
createArray(3628800) : all values are permuted (9->0)
Do you think it's possible/easy to do, and if yes how to proceed ?
[EDIT]
Thanks for helpfull answers
function permute(permutation, val) {
var length = permutation.length,
result = [permutation.slice()],
c = new Array(length).fill(0),
i = 1, k, p,
n = 0;
while (i < length) {
if (c[i] < i) {
if (n <= val) {
k = i % 2 && c[i];
p = permutation[i];
permutation[i] = permutation[k];
permutation[k] = p;
++c[i];
i = 1;
if (n == val) {
arr = permutation.slice();
console.log("n="+n+"\n"+arr);
console.log( 'Duration: '+((new Date() - t1)/1000)+'s' );
break;
}
else { n+=1; }
}
} else {
c[i] = 0;
++i;
}
}
}
let t1 = new Date();
permute([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 100000); // <- array requested
console : n=100000 + 0,5,8,1,7,2,3,6,4,9 + Duration: 0.004s
As this question doesn't decribe a specific programming problem but rather a task, and at that a rather complex one, you shoulnd't expect a full solution as an answer, but i'll try describing a possible method of doing this:
As you said, the number of permutations is 2x3x4x...
You could check if n > 2, if true, then check if n > 2x3, if true check if n > 2x3x4. That way you would know how many of the tailing array indexes you want to permutate. Then you would have to make sure to calculate the permutations in a sorted linear way that doesn't generate the same permutation twice. Thats a math problem, the coding itself should be rather easy (something along the lines of switch the position n times at changing indexes).
Not sure if this is the answer you're looking for, but making a unique permutation algorithm sounds rather complex (see for example this answer to another question https://stackoverflow.com/a/11425168/9521900) which links to this wikipedia article https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order on generating in lexicographic order.
EDIT: From Raj Sharmas comment on your question, this answer on generating permutations seems valuable too:
https://stackoverflow.com/a/37580979/3090583
Related
How to check if an element is present in an array or not in JavaScript without using for loop or any method of array like map, reduce?
let numList= [1,2,3,6,9,2,-8,20]; I want to check for 9 and 30, if it exists or not without using for loop or any array method
I suppose one option would be to convert the array to a Set and check Set.has:
let numList= [1,2,3,6,9,2,-8,20];
const set = new Set(numList);
console.log(set.has(9));
console.log(set.has(30));
Checking whether a Set has an element has complexity of O(1), which is lower complexity than any of the array methods or for loops, which are O(N) or O(log N), so when you have a very large array and you want to check whether it has certain elements, converting it to a Set first can be a good idea.
You can convert the array to string using JSON.stringify and use String includes to check if the string contains the specific searched value
let numList = [1, 2, 3, 6, 9, 2, -8, 20];
let m = JSON.stringify(numList)
console.log(m.includes(-8))
If you can use a while loop you may be able to implement a binary search. Since it was for an interview question I wouldn't be surprised if this is along the lines of what they were looking for.
Here is the traditional algorithm in psuedocode, borrowed from Rosetta Code
BinarySearch(A[0..N-1], value) {
low = 0
high = N - 1
while (low <= high) {
// invariants: value > A[i] for all i < low
value < A[i] for all i > high
mid = (low + high) / 2
if (A[mid] > value)
high = mid - 1
else if (A[mid] < value)
low = mid + 1
else
return mid
}
return not_found // value would be inserted at index "low"
}
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 4 years ago.
I posted about this before trying to use a regular expression, but this time is a bit different.
So I have this list of 500 phone numbers. Here is a little sample of them:
{
8664665844
8885444636
8664604776
8776434327
8887441938
8882642882
8888097429
8668943258
8777711234
8669894327
}
It looks a bit different, this is on my mongoDB server, so its more like an array of objects with a unique uid... so for example:
[
{
_id: kasjf234lkj109euf9023u4n,
field1: 8669894327,
}
]
something like that right.
So basically, my webapp is using a csv file of numbers that you want to compare to your base numbers.
My first thought was using a for, while, for loop kind of thing but im not sure how well it'll work:
for(var i = 0; i < basenums.length; i++){
while ( i >= 0 ){
for(var j = 0; j < comparing.length; j++){
if comparing[j] == basenums.field1[i]{
push that number to a 'dupes' array
}else{
break or something?
}
}
}
}
...this logic is starting to hurt my head...
I know theres an 'includes()' method, but I havn't really used it and when I tried it in this case, It gave me everything as false, even tho the list I was using to compare is just a copy of my 'basenums' list on my server.
What would be the more 'correct' way of doing this?
I'm trying to compare an array with an array. Not a value in its specific array to its own array.
So, rather than marking this as a duplicate, maybe you should re-read:
I have 2 arrays of numbers: [1, 2, 3, 4, 5, 6] and [1, 2, 3, 4, 5, 6] How do I take array a index 0 and compare it to array b every index, then take index 1 of array a and compare to every index in array b, so on and so forth until i > arrayA.length.
Don't try to do everything in one single step. Break your problem into smaller chunks:
Format mongo's response into a hash map that you can efficiently query (O(1))
Test each input number against this map
Step 1:
var hashmap = {}
basenums.forEach(function(basenum) {
hashmap[basenum.field1] = true;
})
Step 2:
var dupes = [];
comparing.forEach(function(comparingNum) {
if(hashmap[comparingNum]) {
dupes.push(comparingNum);
}
})
EDIT: I am not sure, whether you want to have your dupes as a set (unique array). If so, you could use an alternate Step 2:
var dupesMap = {};
comparing.forEach(function(comparingNum) {
if(hashmap[comparingNum]) {
dupesMap[comparingNum] = true;
}
})
var dupes = Object.keys(dupesMap);
You can push values into an array and create a new set with the array. Sets do not keep redundant elements in them. Try this;
let a = ["a","b","a","b","a","b","a","b","a","c"];
console.log(a);
let uniqueA = [...new Set(a)];
console.log(uniqueA);
By the way, for piece of code I got a help from this page: https://medium.com/front-end-hacking/getting-unique-values-in-javascript-arrays-17063080f836
I have a question. I'm looking for a way to get the higest unique number of an array.
var temp = [1, 8, 8, 8, 4, 2, 7, 7];
Now I want to get the output 4 since that is the unique highest number.
Is there a good & hopefully short way to do that?
Yes, there is:
Math.max(...temp.filter(el => temp.indexOf(el) == temp.lastIndexOf(el)))
Explanation:
First, get the elements which are unique in the array using Array#filter
temp.filter(el => temp.indexOf(el) === temp.lastIndexOf(el)) // [1, 4, 2]
Now, get the max of the numbers from the array using ES6 spread operator
Math.max(...array) // 4
This code is equivalent to
Math.max.apply(Math, array);
If you don't want to get fancy, you can use a sort and loop to check the minimal number of items:
var max = 0;
var reject = 0;
// sort the array in ascending order
temp.sort(function(a,b){return a-b});
for (var i = temp.length - 1; i > 0; i--) {
// find the largest one without a duplicate by iterating backwards
if (temp[i-1] == temp[i] || temp[i] == reject){
reject = temp[i];
console.log(reject+" ");
}
else {
max = temp[i];
break;
}
}
Using the spread operator you can find the hightest number easily
Math.max(...numArray);
The only thing left then is to either filter duplicates from the array beforehand, or remove all the elements that match your maximum number if its a duplicate.
remove beforeHand would be easiest in es6 like this.
Math.max(...numArray.filter(function(value){ return numArray.indexOf(value) === numArray.lastIndexOf(numArray);}));
For a non es6 compatible way to remove duplicates have a look at Remove Duplicates from JavaScript Array, the second answer contains an extensive examinations of several alternatives
I need help with the five.myArraysCombined property.
I need it to equal just 1 array (which it currently does in fiddle) and I need it to NOT add any numbers together. (so each number in the array shouldn't be over 20, just like no number in the other arrays are over 20)
http://jsfiddle.net/Dc6HN/1/
For example, if the five arrays are like this
five.myArray1 = [7,2,9,19,3];
five.myArray2 = [6,18,8,1,7];
five.myArray3 = [7,19,4,8,2];
five.myArray4 = [11,9,1,14,5];
five.myArray5 = [3,18,8,9,2];
then the all those arrays combined should be like this
five.myArraysCombined = [7,2,9,19,3,6,18,8,1,7,7,19,4,8,2,11,9,1,14,5,3,18,8,9,2];
Relevant code :
function theNumberClass() {
this.myArray = [[],[],[],[],[]];
this.myArraysCombined = [];
}
var five = new theNumberClass();
function prePickNumbers(objName, theNum, theSumNum, theMaxNum, theMinNum) {
var zzz = [];
for (var x = 0; x < theNum; x += 1) {
pickNumbers(objName.myArray[x], theNum, theSumNum, theMaxNum, theMinNum);
zzz += objName.myArray[x];
}
objName.myArraysCombined.push(zzz);
}
prePickNumbers(five, 5, 40, 20, 1);
My latest attempt was with var zzz and then pushing it to the property, but when I do that it adds up the numbers in the array at times, which is not what I need.
I've also tried several attempts using the .concat(), but it seems to turn it into a string and sometimes also adds up the numbers.
Suppose you have those arrays :
var a = [1, 2, 3]
var b = [4, 5, 6]
var c = [8]
Then you can get a merge of all those with
var all = [].concat.apply([],[a,b,c])
or with
var all = [a,b,c].reduce(function(merged, arr){ return merged.concat(arr) })
In both cases you get
[1, 2, 3, 4, 5, 6, 8]
The first solution is simpler, the second one is more extensible if you want, for example, to remove duplicate or do any kind of filtering/transformation.
I would guess that the issue is the "+=" operator. This operator is used to sum values, not add new elements to an array. Take the following line of code as an example:
zzz += objName.myArray[x];
What I am guessing is that "myArray[x]" is getting added to the value of zzz instead of getting appended to the end of the array. When adding elements to an array in javascript, push is the best option. A better way to write this line is:
zzz.push(objName.myArray[x]);
The question was a bit confusing so I'm not sure if this is what you are looking for but hopefully it will help anyways.
five.reduce(function(o,n){return o.concat(n)},[])
This will reduce the array to a single value, in this case an array of numbers. You can look up Array.reduce() on MDN for more info.
After many hours trying all suggestions left on this thread and another one, and trying multiple other things. I think I finally found a very simple way to do this. And it's the only way I tried that works 100% like I want.
http://jsfiddle.net/Dc6HN/2/
function prePickNumbers(objName, theNum, theSumNum, theMaxNum, theMinNum) {
for (var x = 0; x < theNum; x += 1) {
pickNumbers(objName.myArray[x], theNum, theSumNum, theMaxNum, theMinNum);
objName.myArraysCombined.push(objName.myArray[x]);
}
objName.myArraysCombined = objName.myArraysCombined.toString();
objName.myArraysCombined = objName.myArraysCombined.split(',');
}
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 8 years ago.
Here is my question...
Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array.
For example, given the array [ 1, 2, 4, 4, 3, 3, 1, 5, 3 ] it should return [1, 4, 3]. For extra bonus points return a sorted array.
I am starting out with Javascript - I know the language however, using it in the correct way ( as one should ) I'm still getting to grips with.
My pseudo code for this would be to:
Create an array with the numbers above var numbers = [1, 2, 4, 4, 3, 3, 1, 5, 3];
Then create an empty array named "result" var result = [];
Create a for loop that goes through the var numbers to check for duplicates which will then populate the empty array "result" with the duplicates
for (var i = 0;i < numbers.length; i++) {
//This is where I'm stuck...
}
I'm not sure what to do within the for loop to populate the var result and to throw in to the mix... The given array has to be a function parameter which makes sense so you can change the numbers in one place.
Any feedback on my thought process on this so far is greatly appreciated but ultimately I am wanting to learn how to achieve this.
Here is a JSFiddle of my progress so far... http://jsfiddle.net/fbauW/
One way of doing this (and it's not the only way) is by checking for existing elements in the array. Take a look at JavaScript's lastIndexOf function:
http://www.w3schools.com/jsref/jsref_lastindexof_array.asp
It will return -1 if the object does not exist in your array, and if it exists, will return an index of a later position than you are in. So you can use an if statement in your loop that checks whether or not there is another index containing your number, and add it in to your results array IF AND ONLY IF the index you get back != the index you are currently on (if they equal, this means that there is only one of that element in the list).
If you need more help, comment here and I can type some code in!
Good luck!
Array.prototype.contains = function(k) {
for ( var p in this)
if (this[p] === k)
return true;
return false;
};
//this prototype function checks if an element is already in the array or not
//go through all the array and push the element to result if it is not
//this way we can eliminate duplicates
//result will contain the resultant array
function findDuplicates(Numbers) {
var arrayLength = Numbers.length, i, j, result = [];
for (i = 0; i < arrayLength; i++) {
for (j = 0; j < arrayLength; j++) {
if (a[i] == a[j] && i != j && !result.contains(a[i])) {
result.push(a[i]);
}
}
}
return result;
}