my question is actually similar to: Extracting the most duplicate value from an array in JavaScript (with jQuery)
Solution (the one I found as the best, and slightly changed by me):
var arr = [3, 7, 7, 7, 7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1],
result = {},
max = 0,
res;
for( var i = 0, total = arr.length; i < total; ++i ) {
var val = arr[i],
inc = ( result[val] || 0 ) + 1;
result[val] = inc;
if( inc > max ) {
max = inc;
res = val;
}
}
alert(res);
I would like to add an extra which is : if we have, say two numbers with the same number of occurrences, how could we find the minimum of them (the above should alert 5 and not 7, which is the case)? The current solution works for getting only the first most duplicate found, but it does not deal with repetition.
Thanks!
Sort the array before you count the incidences:
var arr = [3, 7, 7, 7, 7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1];
function min_most_duplicate (arr) {
var result = {},
max = 0,
res;
arr = arr.slice(0); // make a copy of the array
arr.sort(function(a, b) { return (a - b); }); // sort it numerically
for( var i = 0, total = arr.length; i < total; ++i ) {
var val = arr[i],
inc = ( result[val] || 0 ) + 1;
result[val] = inc;
if( inc > max ) {
max = inc;
res = val;
}
}
return res;
}
min_most_duplicate(arr); // returns 5
This works because the way the for loop is written it's going to return the first one that it finds that has the most duplicates, so if the array is sorted, the smallest number will come first, so it'll be the one that the for loop finds.
This would work, example:
var arr = [3, 7, 7, 7, 7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1],
result = {},
max = 0,
res, key, min;
for (var i = 0, total = arr.length; i < total; ++i) {
var val = arr[i],
inc = (result[val] || 0) + 1;
result[val] = inc;
if (inc > max) {
max = inc;
res = val;
}
}
for (var i in result) {
if (result[i] === max) {
key = parseInt(i, 10);
min = min || key;
console.log(min)
if (min > key) {
min = key;
}
}
}
res = min;
alert(res);
let getMostDuplicated = array => {
let duplicated = '';
let max = 0;
for (let i = 0; i < array.length; i++) {
let counter = 0;
for (let j = 1; j < array.length - 1; j++) {
if (array[i] === array[j])
counter++;
}
if (counter > max) {
duplicated = array[i];
max = counter;
}
}
return duplicated;
};
let array = [3, 7, 7, 7, 7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1];
getMostDuplicated(array);
Related
The best and the easiest way to multiply array numbers sequentially
I have got an array with some values:
const arr = [1, 5, 12, 3, 83, 5];
Now I want to get the product of all values from arr. It should works like this: 1 * 5 * 12 * 3 * 83 * 5
I have tried with this code:
const arr = [1, 5, 12, 3, 83, 5];
multiply(arr);
function multiply(arr) {
for(i = 0; i < arr.length; i++) {
product = array[i] * array[i];
console.log(product);
}
}
This code above works like this: 1 * 1, 5 * 5, 12 * 12, 3 * 3, 83 * 83, 5 * 5 and that is not result that I need. I think I know why it works like this but I'm not sure how to write code that I need.
So what's the best option for this kind of tasks?
Edit.
For non-experienced people looking here in future this is the best option that we've found:
Leo Martin answer:
const array = [1, 5, 12, 3, 83, 5];
console.log(multiply(array)); // we log function return value
function multiply(array) {
let score = array[0];
for (i = 1; i < array.length; i++) {
score = score * array[i];
}
return score;
}
and also the shorter version:
By the way, you could use Array.reduce:
const array = [1, 5, 12, 3, 83, 5];
const result = array.reduce((acc, value, index) => {
if (index === 0) return value;
acc = acc * value;
return acc;
}, 0);
console.log(result);
Just move console.log outside for body:
const array = [1, 5, 12, 3, 83, 5];
console.log(multiply(array)); // we log function return value
function multiply(array) {
let score = array[0];
for (i = 1; i < array.length; i++) {
score = score * array[i];
}
return score;
}
By the way, you could use Array.reduce:
const array = [1, 5, 12, 3, 83, 5];
const result = array.reduce((acc, value, index) => {
if (index === 0) return value;
acc = acc * value;
return acc;
}, 0);
console.log(result);
You can use reduce method to multiply all numbers
const array = [1, 5, 12, 3, 83, 5];
const total = array.reduce((total,num) => total * num, 1);
console.log(total)
Corrected your program.
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
var score = 1;
for(i = 0; i < array.length; i++) {
score = score * array[i];
}
console.log(score);
}
Hope the solution is self explanatory. Loop through array. Multiply each product and store the result in the same variable.
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
let product = 1;
for(i = 0; i < array.length; i++) {
product *= array[i];
}
console.log(product);
}
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
var score = array[0];
for(i = 1; i < array.length; i++) {
score = score * array[i];
}
console.log(score);
}
var array = [1, 5, 12, 3, 83, 5];
var result = array.reduce(function(a, b) {
return a * b;
});
console.log(result);
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
for(i = 0; i < array.length-1; i++) {
score = array[i] * array[i+1];
console.log(score);
}
}
For the calculation of the final result of multiply:
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
score = 1;
for(i = 0; i < array.length; i++) {
score = score * array[ i ];
}
console.log(score); // expected result: 74700
}
Are you asking for a textual representation of the calculations? If so, use this:
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
score = '';
for(i = 0; i < array.length; i++) {
score += array[i].toString();
if ( i < array.length -1 ) score += '*';
}
console.log(score); // expected result: '1*5*12*3*83*5'
}
This should start to put you on the right way, hope it helps.
score = array[i] * array[i]; you are trying to multiply exact exp-rations.
const array = [1, 5, 12, 3, 83, 5];
its like: 1*1, 5*5
you can multiply array[i] * i
another approach might be an array function.
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
let product = 1;
`enter code here`for(i = 0; i < array.length; i++) {
product *= array[i];
}
console.log(product);
}
let multiArray = array.map((num, index) => num *index)
console.log(multiArray)
for more info:https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Array/map.
I'm still a junior at web dev and I am trying to solve this problem.
I have to find the number of matching pairs in these arrays:
var ar1 = [10, 20, 20, 10, 10, 30, 50, 10, 20] // return 3 (2 pairs of 10 and 1 pair of 20)
var ar2 = [1, 1, 3, 1, 2, 1, 3, 3, 3, 3] // return 4 (2 pairs of 1 and 2 pairs of 3)
// I started to write my logic below but I'm stuck, could you please help me to solve this problem ?
// The last result I am returning is a filtered array with all the nbs that are superior to 1 and then can't figure out how to get the result of matching pairs :-(
function countPairs(n, ar) {
const count = {};
ar.forEach((nb) => (count[nb] = (count[nb] || 0) + 1));
const values = Object.values(count);
const filter = values.filter((value) => value > 1);
return filter;
}
// 9 and 10 are the length of the arrays
console.log(countPairs(9, ar1))
console.log(countPairs(10, ar2))
Thank you very much for your help!
Perhaps there is a faster/better way to calculate this than this O(2n) solution, but it's something:
var ar1 = [10, 20, 20, 10, 10, 30, 50, 10, 20] // return 3 (2 pairs of 10 and 1 pair of 20)
var ar2 = [1, 1, 3, 1, 2, 1, 3, 3, 3, 3] // return 4 (2 pairs of 1 and 2 pairs of 3)
function countPairs(ar) {
var obj = {};
ar.forEach(item => {
obj[item] = obj[item] ? obj[item] + 1 : 1;
});
return Object.values(obj).reduce((acc, curr) => {
acc += Math.floor(curr / 2)
return acc;
}, 0);
}
console.log(countPairs(ar1))
console.log(countPairs(ar2))
This first calculates the number of occurences for each number and stores them in an Object. Once that is done, we reduce over the values and return the quotient from the division with 2 (to get the number of pairs in total).
Note: I removed the first argument from your function, because the array length is not needed as an argument. It can be obtained from the array you pass directly.
We can achieve this in O(n) time. Maintain an object which keeps track whether a number have been found before, if it was found before, then it makes up a pair, so we increment the pairs count. If not we make the entry of that number in the object 1
function countPairs(arr) {
let pairs = 0;
const obj = {};
arr.forEach(i => {
if (obj[i]) {
pairs += 1;
obj[i] = 0;
} else {
obj[i] = 1;
}
});
return pairs;
}
Simplest solution I can find:
create empty dictionary var t = {}; and use it to count each item in array arr.forEach (i => t[i] = (t[i] || 0) + 1);. After that take all keys Object.values(t) and sum .reduce((acc, p) => acc + ..., 0) each item counts divided by 2 p/2 with Int semantics of course Math.floor(...).
function countPairs(arr) {
var t = {};
arr.forEach (i => t[i] = (t[i] || 0) + 1);
return Object.values(t).reduce((acc, p) => acc + Math.floor(p/2), 0);
}
console.dir(countPairs([1,2,2,2,2,3]));
console.dir(countPairs([1,2,2,2,2,2,3]));
console.dir(countPairs([1,2,2,2,2,2,2,3]));
console.dir(countPairs([10, 20, 20, 10, 10, 30, 50, 10, 20]));
console.dir(countPairs([1, 1, 3, 1, 2, 1, 3, 3, 3, 3]));
First argument in your implementation is not necessary.
Please up-vote if answer was helpful
Concise approach with reduce method
const countPairs = arr => (pairs = [], arr.reduce((p, c) => (p[c] ? (pairs.push([p[c], c]), delete p[c]) : p[c] = c, p), {}), pairs.length)
console.log(countPairs([10, 20, 20, 10, 10, 30, 50, 10, 20]));
So, I wanted a more simpler solution to this problem since I'm just starting to learn to code and I'm teaching my self. I found this solution works perfectly for what you want. I didn't created this solution I found it on the internet(https://www.geeksforgeeks.org/count-equal-element-pairs-in-the-given-array/) I just translated it to JavaScript.
function countDuplicates(n, arr) {
var count = 0;
arr.sort();
for (var i = 0; i < n;) {
if (arr[i] === arr[i + 1]) {
count++;
i = i + 2;
} else {
i++;
}
}
return count;
}
console.log(countDuplicates(9, [10, 20, 20, 10, 10, 30, 50, 10, 20]));
There are some more concise answers here, but here's the solution I have for you:
function countDuplicates(arr) {
var counts = {}, sum = 0;
for (var i = 0; i < arr.length; i++) {
counts[arr[i].toString()] = (counts[arr[i].toString()] || 0) + 1;
}
for (var count in counts) {
if (Object.prototype.hasOwnProperty.call(counts, count)) sum += Math.floor(counts[count] / 2);
}
return sum;
}
console.log(countDuplicates([10, 20, 20, 10, 10, 30, 50, 10, 20]));
I hope I have helped
function numberOfPairs(array) {
let arr = [...array].sort();
let result = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] == arr[i + 1]) {
result++;
arr.shift();
}
}
console.log(result);
}
numberOfPairs(['blue', 'blue', 'blue', 1, 2, 5, 2, 1]);
I've been trying to push a number after the "for loop" into "x" but I cant use "x += count.push()" because it's not an array.
function partsSums(ls) {
let count = 0;
let x = []
while (ls.length > 0) {
for (let i = 0; i < ls.length; i++) {
count += ls[i]
}
x += count;
count = 0;
ls.shift()
}
return x;
}
console.log(partsSums([0, 1, 3, 6, 10]));
I need to get x = [20, 20, 19, 16, 10, 0] and I'm getting 2020191610 instead.
I'm pretty sure this is a no-brainer but I seem to be missing something...
You could reduce from the right.
function partsSums(array) {
return array.reduceRight((r, v) => [v + r[0], ...r], [0]);
}
console.log(partsSums([0, 1, 3, 6, 10]));
Added maximum number according to the input length should be returned.
For example, if the length is 2 then the max among arr[0] + arr[1], arr[1] + arr[2], arr[2] + arr[3] should be returned.
Input is array and length.
I solved this in a real job interview but I think there will be a way not to use nested loop.
const add = (arr, len) => {
let rtnVal = 0
for (let i = len - 1; i < arr.length; i++) {
let temp_idx = i;
let temp_sum = 0;
for (let j = 0; j < len; j++) {
temp_sum = (temp_sum || 0) + arr[temp_idx]
temp_idx -= 1
}
if (temp_sum > rtnVal) {
rtnVal = temp_sum
}
}
return rtnVal
}
console.log(add([1, 2, 3, 4, 5, 6, 7, 8, 9], 4))
I expect the output 30
// enhanced nested loop
const add = (arr, len) => {
let rtnVal = 0;
for(let i=len-1;i<arr.length;i++){
let sum = 0
for(let j=i;j>=i-len+1;j--){
sum += arr[j]
}
if (sum > rtnVal) rtnVal = sum
}
return rtnVal
}
console.log(add([9, 9, 9, 4, 5, 6, 7, 8, 9], 3))
Use a moving window. Add up len numbers starting at the beginning. Then continue through the array adding the next number and subtracting the trailing number.
const add = (arr, len) => {
return arr.reduce((a,v,i,arr) => {
a.running += v;
if(i >= len) {
a.running -= arr[i-len];
}
if(i+1 >= len && a.largest < a.running) {
a.largest = a.running;
}
return a;
}, {
largest: Number.NEGATIVE_INFINITY,
running: 0
}).largest;
}
console.log(add([1,2,3,4,5,6,7,8,9],4)); // 30
console.log(add([-1,-1,-1,-1],2)); // -2
console.log(add([1],1)); // 1
Assuming its sorted like your example. You can use negative slice to select from end and then reduce the array.
const add = (arr, len) => arr.slice(len > arr.len ? arr.len : -len).reduce((total, num) => total + num)
console.log(add([1, 2, 3, 4, 5, 6, 7, 8, 9], 4))
I am trying to find out the minimum elements in array whose sum equals
the given input.I tried for few input sum but was able to find only a
pair in first case while I need to implement for more than just a pair.
var arr = [10, 0, -1, 20, 25, 30];
var sum = 45;
var newArr = [];
console.log('before sorting = ' + arr);
arr.sort(function(a, b) {
return a - b;
});
console.log('after sorting = ' + arr);
var l = 0;
var arrSize = arr.length - 1;
while (l < arrSize) {
if (arr[l] + arr[arrSize] === sum) {
var result = newArr.concat(arr[l], arr[arrSize]);
console.log(result);
break;
} else if (arr[l] + arr[arrSize] > sum) {
arrSize--;
} else {
l++;
}
}
Input Array : [10, 0, -1, 20, 25, 30]
Required Sum: 45
Output: [20, 25]
I am trying for
Required Sum : 59
Output: [10, -1, 20, 30]
This can be viewed as an optimization problem which lends itself well to dynamic programming.
This means you would break it up into a recursion that tries to find the minimum length of increasingly smaller arrays with the sum adjusted for what's been removed. If your array is [10, 0, -1, 20, 25, 30] with a sum of 59 you can think of shortest as the min of:
[10, ... shortest([ 0, -1, 20, 25, 30], 49)
[0, ... shortest([10, 20, 25, 30], 49), 59)
[-1, ... shortest([10, 0, 20, 25, 30], 60)
... continue recursively
with each recursion, the array gets shorter until you are left with one element. Then the question is whether that element equals the number left over after all the subtractions.
It's easier to show in code:
function findMinSum(arr, n){
if(!arr) return
let min
for (let i=0; i<arr.length; i++) {
/* if a number equals the sum, it's obviously
* the shortest set, just return it
*/
if (arr[i] == n) return [arr[i]]
/* recursively call on subset with
* sum adjusted for removed element
*/
let next = findMinSum(arr.slice(i+1), n-arr[i])
/* we only care about next if it's shorter then
* the shortest thing we've seen so far
*/
if (next){
if(min === undefined || next.length < min.length){
min = [arr[i], ...next]
}
}
}
return min && min /* if we found a match return it, otherwise return undefined */
}
console.log(findMinSum([10, 0, -1, 20, 25, 30], 59).join(', '))
console.log(findMinSum([10, 0, -1, 20, 25, 30], 29).join(', '))
console.log(findMinSum([10, 0, -1, 20, 25, 30], -5)) // undefined when no sum
This is still pretty computationally expensive but it should be much faster than finding all the subsets and sums.
One option is to find all possible subsets of the array, and then filter them by those which sum to the required value, and then identify the one(s) with the lowest length:
const getMinElementsWhichSum = (arr, target) => {
const subsets = getAllSubsetsOfArr(arr);
const subsetsWhichSumToTarget = subsets.filter(subset => subset.reduce((a, b) => a + b, 0) === target);
return subsetsWhichSumToTarget.reduce((a, b) => a.length < b.length ? a : b, { length: Infinity });
};
console.log(getMinElementsWhichSum([10, 0, -1, 20, 25, 30], 45));
console.log(getMinElementsWhichSum([10, 0, -1, 20, 25, 30], 59));
// https://stackoverflow.com/questions/5752002/find-all-possible-subset-combos-in-an-array
function getAllSubsetsOfArr(array) {
return new Array(1 << array.length).fill().map(
(e1,i) => array.filter((e2, j) => i & 1 << j));
}
Try this,
var arr = [10, 0, -1, 20, 25, 30];
var sum = 29;
var newArr = [];
var sum_expected = 0;
var y = 0;
while (y < arr.length) {
for (let i = 0; i < arr.length; i++) {
var subArr = [];
sum_expected = arr[i];
if (arr[i] != 0) subArr.push(arr[i]);
for (let j = 0; j < arr.length; j++) {
if (i == j)
continue;
sum_expected += arr[j];
if (arr[j] != 0) subArr.push(arr[j]);
if (sum_expected == sum) {
var result = arr.filter((el)=>(subArr.indexOf(el) > -1));
!newArr.length ? newArr = result : result.length < newArr.length ? newArr = result : 1;
break;
}
}
}
let x = arr.shift();
arr.push(x);
y++;
}
if (newArr.length) {
console.log(newArr);
} else {
console.log('Not found');
}