nodejs vs javascript (Two different answers) - javascript

var largestNumber = function (nums) {
let comp = (a, b) => {
a = a.split("").reverse().join("");
b = b.split("").reverse().join("");
return a.localeCompare(b) > 0 ? 1 : 0;
};
return nums.map(v => '' + v).sort(comp).reverse().join('');
};
console.log(largestNumber([3, 30, 34, 5, 9]));
In nodejs
output: 9534330
In javascript
output: 9534303
What is happening?

As said in the comments your compare function is non deterministic and the 2 engines result in different outcomes. You could try the following in both versions to see consistent results 9534330.
var largestNumber = function (nums) {
let comp = (a, b) => {
a = a.split("").reverse().join("");
b = b.split("").reverse().join("");
return a.localeCompare(b);
};
return nums.map(v => '' + v).sort(comp).reverse().join('');
};
console.log(largestNumber([3, 30, 34, 5, 9]));

Related

Cloning filter function using ternary operator

I need help implementing my own version of the .filter() method using a ternary operator.
const s = [23, 65, 98, 5];
Array.prototype.myFilter = function(callback) {
this.forEach(a => callback(a) === true ? newArray.push(a) : //??? )
return newArray;
};
var new_s = s.myFilter(function(item) {
return item % 2 === 1;
});
How do I complete this? What do I put in place of "//???"?
You could use null if callback(a) is not true. Also, newArray is not defined in your example.
var s = [23, 65, 98, 5];
Array.prototype.myFilter = function(callback) {
const newArray = []
this.forEach(a => callback(a) === true ? newArray.push(a) : null )
return newArray;
};
var new_s = s.myFilter(function(item) {
return item % 2 === 1;
});
console.log("s:", s)
console.log("new_s:", new_s)
Here, the use of a ternary operator is not necessary, or even a hindrance since you do not need the else branch. There is also the question if the result of callback really has to be true, or if truthy is enough. The auxiliary array is also obsolete.
const s = [23, 65, 98, 5];
Array.prototype.myFilter = function(callback) {
return this.reduce((accumulator, currentValue) => {
if (callback(currentValue)) {
accumulator.push(currentValue)
}
return accumulator
}, []);
};
const new_s = s.myFilter((item) => item % 2 === 1);
console.log("s:", s)
console.log("new_s:", new_s)

Return a nested array

I need help making this function after taking an array and another array (duplicate) that has just the numbers that are duplicated in the first array (for example array=[1,2,3,1,2,3,4,5,6,6], duplicate=[1,2,3,6]).
I want it to return an array as follow: finalArray1=[[1,1],[2,2],[3,3],4,5,[6,6]].
let input = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];
let sortArray = array => {
return array.sort(function(a, b) {
return a - b;});
}
function findDuplicates(data) {
let duplicate = [];
data.forEach(function(element, index) {
// Find if there is a duplicate or not
if (data.indexOf(element, index + 1) > -1) {
// Find if the element is already in the duplicate array or not
if (duplicate.indexOf(element) === -1) {
duplicate.push(element);
}
}
});
return duplicate;
}
let newArray = (array, duplicate) => {
for( var i = 0; i < 3; i++ ){
for( var j = 0; j < 15; j++ ){
if( duplicate[i] == array[j] ){
let finalArray = new array().push(array[j]);
}
}
return finalArray;
}
}
You could take a Map and return in original order.
The map takes the items in insertation order, which means all item are later in original order. If a key exists, it createsd an array with the value. Otherwise just the item form the array is taken as value for the map.
At the end take only the values from the map and create an array of it.
let input = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20],
result = Array.from(input
.reduce((m, v) => m.set(v, m.has(v) ? [].concat(m.get(v), v) : v), new Map)
.values()
);
console.log(result);
A more traditional approach
let input = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20],
result = input
.sort((a, b) => a -b)
.reduce((r, v, i, a) => {
if (a[i - 1] !== v && v !== a[i + 1]) r.push(v); // check if unique
else if (a[i - 1] !== v) r.push([v]); // check left element
else r[r.length - 1].push(v);
return r;
}, []);
console.log(result);
I think the following can work for you:
const input = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];
const inOrder = input.sort((a, b) => a - b);
const unique = inOrder.reduce((a, c) => {
const found = a.find(e => e.includes(c));
if (found) found.push(c);
else a.push([c]);
return a;
}, []);
const result = unique.map(e => e.length > 1 ? e : e[0]);
console.log(result);
I hope this helps!

Given a List of Integers, find the most frequently occurring integer in the list

I had this come up in an interview recently. Given a list of integers find the most frequently occurring integer in the list and return in an array. I was able to answer, but one of the test cases threw me off.
I'm stuck on what kind of sorting method I should use to sort the the array for the most frequent integer and return all items in the array that contain the most common integer.
const arr = [1,1,2,3,3,3,3,4,5,5,10]; // return 3
const mostCommon = (arr) => {
if (arr.length < 1 ) {
return null;
}
const map = {};
let mostFrequentNum = arr[0];
for (let i = 0; i < arr.length; i++) {
let currentNum = arr[i];
!map[currentNum] ? map[currentNum] = 1 : ++map[currentNum];
// 1. Current has higher count than known max
if(map[currentNum] > map[mostFrequentNum]) {
mostFrequentNum = currentNum;
}
}
return mostFrequentNum;
};
mostCommon(arr); // return 3
/* confused how to implement test case below */
// const arr = [5, 99, 3994813, 99, -32, 43, 99, 3994813, 3994813];
// return [ 99, 3994813 ]
Just accumulate the counts like you're doing, and then after that's finished go through and find the biggest count.
const arr = [1,1,2,3,3,3,3,4,5,5,10];
const mostCommon = (arr) => {
const map = {};
for (currentNum of arr) {
!map[currentNum] ? map[currentNum] = 1 : ++map[currentNum];
}
let result = Object.keys(map).reduce((r, n) => {
if (map[n] > r.c) {
r.c = map[n];
r.n = n;
}
return r;
}, { c: -1 });
return result.n;
};
console.log(mostCommon(arr));
console.log(mostCommon([5, 99, 3994813, 99, -32, 43, 99, 3994813, 3994813]));
The .reduce() process keeps an object that includes fields for the number from the original array, and its count.
Your question uses "integer" singular but your examples suggest you'd like the list of values that tie for the largest count. To do that, you'd modify the above to maintain the .reduce() accumulator with a list instead of just a simple scalar:
const arr = [1,1,2,3,3,3,3,4,5,5,10];
const mostCommon = (arr) => {
const map = {};
for (currentNum of arr) {
!map[currentNum] ? map[currentNum] = 1 : ++map[currentNum];
}
let result = Object.entries(map).reduce((r, [n, c]) => {
if (c > r.max) r.max = c;
r[c] ? r[c].push(n) : r[c] = [n];
return r;
}, { max: -1 });
return result[result.max];
};
console.log(mostCommon(arr));
console.log(mostCommon([5, 99, 3994813, 99, -32, 43, 99, 3994813, 3994813]));
I have only 1 solution ;)
const arr = [1,1,2,3,3,3,3,4,5,5,10];
var
counting = {},
most_Freq = arr[0]
;
arr.forEach( x=>{ counting[x] = (counting[x] || 0)+1; });
most_Freq = Object.keys(counting).reduce((a, b) => counting[a] > counting[b] ? a : b);
console.log ( 'most frequent is ', most_Freq )
Use Array.reduce() to create a Map of numbers with frequencies.
Use Math.max() get highest frequency from the Map.values().
Convert the Map to an array of entries ([key, value]), filter out the items with frequency lower than the highest frequency, and map to a list of numbers.
const arr = [1, 1, 2, 3, 3, 3, 3, 4, 5, 5, 10];
const mostCommon = arr => {
const freqMap = arr.reduce((r, n) => r.set(n, (r.get(n) || 0) + 1), new Map); // create a Map of number by frequency
const highestFreq = Math.max(...freqMap.values()); // get the highest frequency number
return Array.from(freqMap) // convert the Map to an array of entries
.filter(([, v]) => v === highestFreq) // filter lower frequency items
.map(([k]) => k); // convert back to an array of numbers
}
console.log(mostCommon(arr));
console.log(mostCommon([5, 99, 3994813, 99, -32, 43, 99, 3994813, 3994813]));

One-Dimensional Array Iteration Using Recursion

I'm trying to iterate over a simple array using recursion. For this specific case, I'm trying to recreate .map() using recursion (without using .map()!. I currently am only pushing the last element in the original array, but I want to push all into the array.
function recursiveMap (arr, func) {
let newArr = [];
if (arr.length === 1){
newArr.push(func(arr));
}
else {
newArr.push(...recursiveMap(arr.slice(1),func));
}
return newArr;
}
You need to to use func on the current item, and spread the result of calling the function on the rest of the array:
function recursiveMap(arr, func) {
return arr.length ? [func(arr[0]), ...recursiveMap(arr.slice(1), func)] : [];
}
const arr = [1, 2, 3];
const result = recursiveMap(arr, n => n * 2);
console.log(result);
Your base case seems wrong. You will need to check for an empty array:
function recursiveMap (arr, func) {
let newArr = [];
if (arr.length === 0) {
// do nothing
} else {
newArr.push(func(arr[0]));
newArr.push(...recursiveMap(arr.slice(1),func));
}
return newArr;
}
Instead you will need to call func (on the first item) when there is at least one element.
With recursion, I find it is helpful to have the base case be the very first thing you check in your function, and short the execution there. The base case for map is if the array has 0 items, in which case you would return an empty array.
if you haven't seen it before let [a, ...b] is array destructuring and a becomes the first value with b holding the remaining array. You could do the same with slice.
function recursiveMap(arr, func){
if(arr.length == 0) return [];
let [first, ...rest] = arr;
return [func(first)].concat(recursiveMap(rest, func));
}
let test = [1,2,3,4,5,6,7];
console.log(recursiveMap(test, (item) => item * 2));
EDIT
Going back to your sample I see you clearly have seen destructuring before xD, sorry. Leaving it in the answer for future readers of the answer though.
Below are a few alternatives. Each recursiveMap
does not mutate input
produces a new array as output
produces a valid result when an empty input is given, []
uses a single pure, functional expression
Destructuring assignment
const identity = x =>
x
const recursiveMap = (f = identity, [ x, ...xs ]) =>
x === undefined
? []
: [ f (x), ...recursiveMap (f, xs) ]
const square = (x = 0) =>
x * x
console.log (recursiveMap (square, [ 1, 2, 3, 4, 5 ]))
// [ 1, 4, 9, 16, 25 ]
Array slice
const identity = x =>
x
const recursiveMap = (f = identity, xs = []) =>
xs.length === 0
? []
: [ f (xs[0]), ...recursiveMap (f, xs.slice (1)) ]
const square = (x = 0) =>
x * x
console.log (recursiveMap (square, [ 1, 2, 3, 4, 5 ]))
// [ 1, 4, 9, 16, 25 ]
Additional parameter with default assignment – creates fewer intermediate values
const identity = x =>
x
const recursiveMap = (f = identity, xs = [], i = 0) =>
i >= xs.length
? []
: [ f (xs[i]) ] .concat (recursiveMap (f, xs, i + 1))
const square = (x = 0) =>
x * x
console.log (recursiveMap (square, [ 1, 2, 3, 4, 5 ]))
// [ 1, 4, 9, 16, 25 ]
Tail recursive (and cute)
const identity = x =>
x
const prepend = x => xs =>
[ x ] .concat (xs)
const compose = (f, g) =>
x => f (g (x))
const recursiveMap = (f = identity, [ x, ...xs ], then = identity) =>
x === undefined
? then ([])
: recursiveMap
( f
, xs
, compose (then, prepend (f (x)))
)
const square = (x = 0) =>
x * x
console.log (recursiveMap (square, [ 1, 2, 3, 4, 5 ]))
// [ 1, 4, 9, 16, 25 ]
// => undefined
recursiveMap (square, [ 1, 2, 3, 4, 5 ], console.log)
// [ 1, 4, 9, 16, 25 ]
// => undefined
recursiveMap (square, [ 1, 2, 3, 4, 5 ])
// => [ 1, 4, 9, 16, 25 ]
Derived from tail-recursive foldl – Note foldl chooses a similar technique used above: additional parameter with default assignment.
const identity = x =>
x
const foldl = (f = identity, acc = null, xs = [], i = 0) =>
i >= xs.length
? acc
: foldl
( f
, f (acc, xs[i])
, xs
, i + 1
)
const recursiveMap = (f = identity, xs = []) =>
foldl
( (acc, x) => acc .concat ([ f (x) ])
, []
, xs
)
const square = (x = 0) =>
x * x
console.log (recursiveMap (square, [ 1, 2, 3, 4, 5 ]))
// [ 1, 4, 9, 16, 25 ]
You could take another approach by using a third parameter for the collected values.
function recursiveMap(array, fn, result = []) {
if (!array.length) {
return result;
}
result.push(fn(array[0]));
return recursiveMap(array.slice(1), fn, result);
}
console.log(recursiveMap([1, 2, 3, 4, 5], x => x << 1));
console.log(recursiveMap([], x => x << 1));
welcome to Stack Overflow. You could either pass result to itself like in the following example:
function recursiveMap (arr, func,result=[]) {
if (arr.length === 0){
return result;
}
return recursiveMap(
arr.slice(1),
func,
result.concat([func(arr[0])])
);
}
console.log(recursiveMap([1,2,3,4],x=>(x===3)?['hello','world']:x+2));
Or define a recursive function in your function:
function recursiveMap (arr, func) {
const recur = (arr, func,result=[])=>
(arr.length === 0)
? result
: recur(
arr.slice(1),
func,
result.concat([func(arr[0])])
);
return recur(arr,func,[])
}
console.log(recursiveMap([1,2,3,4],x=>(x===3)?['hello','world']:x+2));
Add newArr.push(func(arr[0])); before calling again the function
function recursiveMap (arr, func) {
let newArr = [];
if (arr.length === 1){
newArr.push(func(arr));
}
else {
newArr.push(func(arr[0]));
newArr.push(...recursiveMap(arr.slice(1),func));
}
return newArr;
}
console.log(recursiveMap([1,2,3], function(a){return +a+2}))
Same but modified answer with bugs corrected
function recursiveMap (arr, func) {
let newArr = [];
if(arr.length){
newArr.push(func(arr[0]));
if(arr.length > 1){
newArr.push(...recursiveMap(arr.slice(1),func));
}
}
return newArr;
}
console.log(recursiveMap([1,2,3], function(a){return a+2}))

How do you split an array into array pairs in JavaScript?

I want to split an array into pairs of arrays.
var arr = [2, 3, 4, 5, 6, 4, 3, 5, 5]
would be
var newarr = [
[2, 3],
[4, 5],
[6, 4],
[3, 5],
[5]
]
You can use js reduce
initialArray.reduce(function(result, value, index, array) {
if (index % 2 === 0)
result.push(array.slice(index, index + 2));
return result;
}, []);
Lodash has a method for this: https://lodash.com/docs/4.17.10#chunk
_.chunk([2,3,4,5,6,4,3,5,5], 2);
// => [[2,3],[4,5],[6,4],[3,5],[5]]
There's no pre-baked function to do that, but here's a simple solution:
var splitPairs = function(arr) {
var pairs = [];
for (var i=0 ; i<arr.length ; i+=2) {
if (arr[i+1] !== undefined) {
pairs.push ([arr[i], arr[i+1]]);
} else {
pairs.push ([arr[i]]);
}
}
return pairs;
};
Yet another that's a bit of a mish-mash of the already-posted answers. Adding it because having read the answers I still felt things could be a little easier to read:
var groups = [];
for(var i = 0; i < arr.length; i += 2)
{
groups.push(arr.slice(i, i + 2));
}
There is now the flexible Array#flatMap(value, index, array):
const pairs = arr.flatMap((_, i, a) => i % 2 ? [] : [a.slice(i, i + 2)]);
And the possibly more efficient, but goofy looking Array.from(source, mapfn?):
const pairs = Array.from({ length: arr.length / 2 }, (_, i) => arr.slice(i * 2, i * 2 + 2))
It's possible to group an array into pairs/chunks in one line without libraries:
function chunks(arr, size = 2) {
return arr.map((x, i) => i % size == 0 && arr.slice(i, i + size)).filter(x => x)
}
console.log(chunks([1, 2, 3, 4, 5, 6, 7])) // -> [[1, 2], [3, 4], [5, 6], [7]]
Here's a good generic solution:
function splitInto(array, size, inplace) {
var output, i, group;
if (inplace) {
output = array;
for (i = 0; i < array.length; i++) {
group = array.splice(i, size);
output.splice(i, 0, group);
}
} else {
output = [];
for (i = 0; i < array.length; i += size) {
output.push(array.slice(i, size + i));
}
}
return output;
}
For your case, you can call it like this:
var arr= [2,3,4,5,6,4,3,5,5];
var newarr = splitInto(arr, 2);
The inplace argument determines whether the operation is done in-place or not.
Here's a demo below:
function splitInto(array, size, inplace) {
var output, i, group;
if (inplace) {
output = array;
for (i = 0; i < array.length; i++) {
group = array.splice(i, size);
output.splice(i, 0, group);
}
} else {
output = [];
for (i = 0; i < array.length; i += size) {
output.push(array.slice(i, size + i));
}
}
return output;
}
var arr= [2,3,4,5,6,4,3,5,5];
var newarr = splitInto(arr, 2);
disp(newarr);
// or we can do it in-place...
splitInto(arr, 3, true);
disp(arr);
function disp(array) {
var json = JSON.stringify(array);
var text = document.createTextNode(json);
var pre = document.createElement('pre');
pre.appendChild(text);
document.body.appendChild(pre);
}
A slightly different approach than using a for loop for comparison. To avoid modifying the original array slice makes a shallow copy since JS passes objects by reference.
function pairArray(a) {
var temp = a.slice();
var arr = [];
while (temp.length) {
arr.push(temp.splice(0,2));
}
return arr;
}
var array = [2,3,4,5,6,4,3,5,5];
var newArr = pairArray(array);
function pairArray(a) {
var temp = a.slice();
var arr = [];
while (temp.length) {
arr.push(temp.splice(0,2));
}
return arr;
}
document.write('<pre>' + JSON.stringify(newArr) + '</pre>');
I would use lodash for situations like this.
Here is a solution using _.reduce:
var newArr = _(arr).reduce(function(result, value, index) {
if (index % 2 === 0)
result.push(arr.slice(index, index + 2));
return result;
}, []);
var arr = [2,3,4,5,6,4,3,5,5];
var newArr = _(arr).reduce(function(result, value, index) {
if (index % 2 === 0)
result.push(arr.slice(index, index + 2));
return result;
}, []);
document.write(JSON.stringify(newArr)); // [[2,3],[4,5],[6,4],[3,5],[5]]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js"></script>
Here's another solution using lodash helpers:
function toPairs(array) {
const evens = array.filter((o, i) => i % 2);
const odds = array.filter((o, i) => !(i % 2));
return _.zipWith(evens, odds, (e, o) => e ? [o, e] : [o]);
}
console.log(toPairs([2,3,4,5,6,4,3,5,5]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
const items = [1, 2, 3, 4, 5];
const createBucket = (bucketItems, bucketSize) => buckets => {
return bucketItems.length === 0 ? buckets : [...buckets, bucketItems.splice(0, bucketSize)];
};
const bucketWithItems = items.reduce(createBucket([...items], 4), []);
Here is a short and more generic solution:
function splitArrayIntoPairs(arr, n) {
var len = arr.length
var pairs = []
for (let i = 0; i < len; i += n) {
var temp = []
for (var j = i; j < (i + n); j++) {
if (arr[j] !== undefined) {
temp.push(arr[j])
}
}
pairs.push(temp)
}
return pairs
}
Where arr is your array and n is no of pairs
This combines some of the answers above but without Object.fromEntires. The output is similar to what you would get with minimist.
const splitParameters = (args) => {
const split = (arg) => (arg.includes("=") ? arg.split("=") : [arg]);
return args.reduce((params, arg) => [...params, ...split(arg)], []);
};
const createPairs = (args) =>
Array.from({ length: args.length / 2 }, (_, i) =>
args.slice(i * 2, i * 2 + 2)
);
const createParameters = (pairs) =>
pairs.reduce(
(flags, value) => ({
...flags,
...{ [value[0].replace("--", "")]: value[1] }
}),
{}
);
const getCliParameters = (args) => {
const pairs = createPairs(splitParameters(args));
const paramaters = createParameters(pairs);
console.log(paramaters);
return paramaters;
};
//const argsFromNodeCli = process.argv.slice(2); // For node
const testArgs = [
"--url",
"https://www.google.com",
"--phrases=hello,hi,bye,ok"
];
const output = getCliParameters(testArgs);
document.body.innerText = JSON.stringify(output);
Here is another concise but still efficient solution using modern JavaScript (arrow function, Array.prototype.at):
splitPairs = arr =>
arr.reduce((pairs, n, i) =>
(i % 2 ? pairs.at(-1).push(n)
: pairs.push([n]),
pairs), []);
It is (memory-)efficient because it just creates one array for the result and one array for each pair and then modifies them. The case where there is an odd number of elements is handled naturally.
When minified, it is also really concise code:
splitPairs = a=>a.reduce((p,n,i)=>(i%2?p.at(-1)[1]=n:p.push([n]),p),[]);
Using ES6 features:
const arr = [2, 3, 4, 5, 6, 4, 3, 5, 5]
const result = arr.slice(arr.length/2).map((_,i)=>arr.slice(i*=2,i+2))
console.log(result)
Here is another generic solution that uses a generator function.
/**
* Returns a `Generator` of all unique pairs of elements from the given `iterable`.
* #param iterable The collection of which to find all unique element pairs.
*/
function* pairs(iterable) {
const seenItems = new Set();
for (const currentItem of iterable) {
if (!seenItems.has(currentItem)) {
for (const seenItem of seenItems) {
yield [seenItem, currentItem];
}
seenItems.add(currentItem);
}
}
}
const numbers = [1, 2, 3, 2];
const pairsOfNumbers = pairs(numbers);
console.log(Array.from(pairsOfNumbers));
// [[1,2],[1,3],[2,3]]
What I like about this approach is that it will not consume the next item from the input until it actually needs it. This is especially handy if you feed it a generator as input, since it will respect its lazy execution.

Categories