Find all possible permutations of given array and its subarrays - javascript

I'm sure this has been answered somewhere, but I can't find an answer for this specific problem with JS before ES5.
I need a function like this:
function testAllPossibilities(array, callback) {
// example array = [Obj1,Obj2];
// allPossibilites = [
// [Obj1],
// [Obj2],
// [Obj1,Obj2],
// [Obj2,Obj1]
// ]
for (var i = 0; i < allPossibilites.length; i++) {
callback(allPossibilites[i]);
}
}
I'm looking for all permutations without repetitions for the original input array and its sub-arrays.

function combinate (array) {
const possibilities = new Set();
function lookup (possible) {
if possible.length {
possibilities.add(possible);
}
const p = new Set(possible);
const further = array.filter(el => !p.has(el));
for (let entry of further) {
lookup(possible.concat(entry));
}
}
lookup([]);
return Array.from(possibilities);
}
Do not run this with more than 8 elements.

Related

How to refactor to a higher order function?

How to refactor this function to a higher order function?
It is meant to return a new array containing the sub-arrays of characters that contain the value 'Rambo'.
function isRamboCharacter(characters) {
const x = [];
for (let i = 0; i < characters.length; i++) {
if (characters[i].movie.includes('Rambo')) {
x.push(characters[i]);
}
}
return x;
}
I tried:
return characters.movie.includes('Rambo');
Solution
const characters = [
{ movie: ["Rambo", "Rockey", "Avengers"] },
{ movie: ["Fatherhood", "Rockey", "Avengers"] }
]
const isRamboCharacter = (characters) => characters.filter((char) => char.movie.includes("Rambo"))
console.log(isRamboCharacter(characters));
Or you could directly call Array's filter method
console.log(characters.filter((char) => char.movie.includes("Rambo")));
//output - [ { movie: [ 'Rambo', 'Rockey', 'Avengers' ] } ]
You can curry it (Return a function in a function)
function isCharacterInMovie(movie) {
return function filterFunction(characters) {
const x = [];
for (let i = 0; i < characters.length; i++) {
if (characters[i].movie.includes(movie)) {
x.push(characters[i]);
}
}
return x;
}
}
const isRamboInMovie = isCharacterInMovie('rambo') // returns the inner function which holds on to the value from when you called it
isRamboInMovie(charactersInTheMatrix); // []
isRamboInMovie(charactersInRambo); // ['rambo']
The 'You don't know JS' book series has a great (short AND free) book on this, and it explains it incredibly well
You Don't Know JS Yet: Scope & Closures - 2nd Edition

How do I recursively call a prototype function inside itself while looping an array in JavaScript?

I'm creating a polyfill of Array.flat() method, however, I'm facing issues while calling the function within itself after checking that the looped element is an array and thats need to be flattened further. When a write a code that is not prototypal, the flattening is proper, however when I try to create a prototype function, I'm unable to get the flattened array. I'm pretty sure that the issue is related with the 'this' keyword. Please have a look at my code.
Here is the code
let arrayFlat = [1, 2, 3, [4, 5, 6, [7, 8, [9]], 10, [11, 12]], [13, [14, 15]]];
const flatArray = (array) => {
let output = [];
const flatten = (array) => {
for (let i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
flatten(array[i]);
} else {
output.push(array[i]);
}
}
return output;
};
return flatten(array);
};
Array.prototype.myFlat = function () {
let output = [];
for (let i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
console.log(this[i]);
this[i].myFlat();
} else {
output.push(this[i]);
}
}
return output;
};
In your first piece of code, you create a single output array. When you recursively call flatten, the code is always pushing to the exact same output array, which is in the closure of flatten. Then once everything is done, you return that array.
In the second code, you create a new array every time you recurse. Each recursion will create an array, flatten itself, and then return that new array. But the return value is ignored, so these values don't go anywhere.
You have a few options
Make the code basically identical to your first one, with an internal function for doing the recursion, and a closure variable used by all:
Array.prototype.myFlat = function () {
let output = [];
const flatten = (array) => {
for (let i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
flatten(array[i]);
} else {
output.push(array[i]);
}
}
return output;
};
return flatten(this);
}
Pass the output array as a parameter when you recurse:
// VVVVVV--- added parameter
Array.prototype.myFlat = function (output = []) {
for (let i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
this[i].myFlat(output); // <---- forward the array along
} else {
output.push(this[i]);
}
}
return output;
};
Continue having separate arrays, but then merge them together as the stack unwinds:
Array.prototype.myFlat = function () {
let output = [];
for (let i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
output.push(...this[i].myFlat()); // <---- added output.push
} else {
output.push(this[i]);
}
}
return output;
};
I am a strong proponent of keeping classes as thin as possible, wrapping functional interfaces wherever possible -
function myFlat(t) {
return Array.isArray(t)
? t.reduce((r, v) => r.concat(myFlat(v)), [])
: [t]
}
Array.prototype.myFlat = function() { return myFlat(this) }
console.log([1,[2,[3],4],[[5]],6,[[[7]]]].myFlat())
// [ 1, 2, 3, 4, 5, 6, 7 ]

How do I remove multiple elements from an array?

I want to write a function that passes an array and an optional number of values to be removed from the array as parameters. My function works when there is only 1 value, but fails when there is multiple values.
const removeFromArray = function (arr, ...theArgs) {
for (let i = 0; i < arr.length; i++) {
if (theArgs.includes(arr[i])) {
arr.splice(i, 1);
}
}
return arr;
};
You can use the filter method for that:
const removeFromArray = function (arr, ...theArgs) {
return arr.filter( val => !theArgs.includes(val) )
};
const list = [1,2,3];
const newList = removeFromArray(list, 2,3);
console.log(newList);
And a more terse version:
const removeFromArray = (arr, ...args)=> arr.filter( val => !args.includes(val) )
Tip: try to avoid mutating the original array and work on or return a copy during these operations.
The issue is with your indexing, you are finding the element using the index of arr, and deleting in the array, which is probably causing issue with indexing in loop.
Modify your code as follows
const removeFromArray = function (arr, ...theArgs) {
for (let i = 0; i < theArgs.length; i++) {
if (arr.includes(theArgs[i])) {
arr.splice(arr.indexOf(theArgs[i]), 1);
}
}
return arr;
};
The above fixes the code your way, but a better way of doing it would be using filter.
const removeFromArray = function (arr, ...theArgs) {
return arr.filter(ele => !theArgs.includes(ele))
}
I am writing it this way to purely maintain your function.
The problem is because you remove item from the array while being looping from that array.
Every time your for loop iterate the array, it will get a new array
e.g. (1,2,3,4,5 => 2,3,4,5), but the i value just keeping increasing by 1.
const removeFromArray = function (arr, ...theArgs) {
for (let i = 0; i < arr.length; i++) {
console.log(`arr:${arr}`,`item${arr[i]}`,`num${i}`)
console.log(arr[i])
if (theArgs.includes(arr[i])) {
arr.splice(i, 1);
}
}
return arr;
};
const testarray = [1,2,3,4,5]
console.log(removeFromArray(testarray,1,2,3))
I would suggest to use array as a second parameter.
var array1 = ['a','b','c'];
var elementsToRemove = ['a','b'];
const removeFromArray = function (array1, elementsToRemove) {
var filtered = array1.filter(function(value, index, array){
return elementsToRemove.includes(value);
});
return filtered;
}
console.log(removeFromArray(array1,elementsToRemove));

get 2 array unique and duplicate elements from parent array

let array=[1,1,2,3,4,4,5,5,6,6,7,8]:
I want to get 2 array of unique and duplicate elements: duplicate element's array1:[1,4,5,6] unique element's array2:[2,3,7,8]
Help is very much appreciated.
Simply use a map to filter out duplicates. Here's a working solution.
let arr= [1,1,2,3,4,4,5,5,6,6,7,8];
function uniqueAndDuplicates() {
var obj = {};
var duplicates = [];
var noDup = [];
for(var i = 0; i < arr.length; i++){
if(!obj[arr[i]]){
obj[arr[i]] = 1;
noDup.push(arr[i]);
} else {
duplicates.push(arr[i]);
}
}
var unique = noDup.filter(function(item, index){
if(!duplicates.includes(noDup[index])){
return noDup[index];
}
});
return {
unique,
duplicates
}
}
console.log(uniqueAndDuplicates(arr));
One solution is you can create a freq map and then you can essentially create an Array from that map then filter out the counts greater than 1 and less than 1.
let array=[1,1,2,3,4,4,5,5,6,6,7,8]
const returnUniqueAndDuplicateElements = (arr) => {
let freqMap = {};
for(const idx in arr){
let number = arr[idx];
if(!freqMap[number]){
freqMap[number]=0;
}
freqMap[number]++;
}
const entries = Object.entries(freqMap);
return {
unique: entries.filter(([, freq]) => freq <= 1).map(item => Number(item[0])),
duplicates: entries.filter(([, freq]) => freq > 1).map(item => Number(item[0]))
}
}
console.log(returnUniqueAndDuplicateElements(array))
I am adding my solution since there is none with JS sets[?] just yet:
function uniqueAndDuplicate(arrayOfNumbers) {
const unique = new Set();
const duplicate = new Set();
arrayOfNumbers.forEach((elem) => {
if (unique.has(elem)) {
unique.delete(elem);
duplicate.add(elem);
} else if (!duplicate.has(elem)) {
unique.add(elem);
}
});
return {
unique: [...unique],
duplicate: [...duplicate],
};
}
console.log(uniqueAndDuplicate([1,1,2,3,4,4,5,5,6,6,7,8]));

Removing duplicate element in an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easiest way to find duplicate values in a JavaScript array
Javascript array sort and unique
I have the following array
var output = new array(7);
output[0]="Rose";
output[1]="India";
output[2]="Technologies";
output[3]="Rose";
output[4]="Ltd";
output[5]="India";
output[6]="Rose";
how can i remove the duplicate elements in above array.Is there any methods to do it?
You can write a function like this
function eliminateDuplicates(arr) {
var i,
len=arr.length,
out=[],
obj={};
for (i=0;i<len;i++) {
obj[arr[i]]=0;
}
for (i in obj) {
out.push(i);
}
return out;
}`
Check this here
Maybe more complex than you need but:
function array_unique (inputArr) {
// Removes duplicate values from array
var key = '',
tmp_arr2 = {},
val = '';
var __array_search = function (needle, haystack) {
var fkey = '';
for (fkey in haystack) {
if (haystack.hasOwnProperty(fkey)) {
if ((haystack[fkey] + '') === (needle + '')) {
return fkey;
}
}
}
return false;
};
for (key in inputArr) {
if (inputArr.hasOwnProperty(key)) {
val = inputArr[key];
if (false === __array_search(val, tmp_arr2)) {
tmp_arr2[key] = val;
}
}
}
return tmp_arr2;
}
Code taken from: http://phpjs.org/functions/array_unique:346
You can remove dups from an array by using a temporary hash table (using a javascript object) to keep track of which images you've already seen in the array. This works for array values that can be uniquely represented as a string (strings or numbers mostly), but not for objects.
function removeDups(array) {
var index = {};
// traverse array from end to start
// so removing the current item from the array
// doesn't mess up the traversal
for (var i = array.length - 1; i >= 0; i--) {
if (array[i] in index) {
// remove this item
array.splice(i, 1);
} else {
// add this value to index
index[array[i]] = true;
}
}
}
Here's a working example: http://jsfiddle.net/jfriend00/sVT7g/
For sizable arrays, using an object as a temporary index will be many times faster than a linear search of the array.
First of all, you'll want to use the array literal (var output = []) to declare your array. Second, you'll want to loop through your array and store all the values in a second array. If any value in the first array matches a value in the second array, delete it and continue looping.
Your code would look like this:
var output = [
"Rose",
"India",
"Technologies",
"Rose",
"Ltd",
"India",
"Rose"
]
var doubledOutput = [];
for(var i = 0; i < output.length; i++) {
var valueIsInArray = false;
for(var j = 0; j < doubledOutput.length; j++) {
if(doubledOutput[j] == output[i]) {
valueIsInArray = true;
}
}
if(valueIsInArray) {
output.splice(i--, 1);
} else {
doubledOutput.push(output[i]);
}
}
Please note, the above code is untested and may contain errors.

Categories