how can I sum all same elements in one array? For example I have an array:
[20,20,20,10,10,5,1]
How can I make it [60,20,5,1]?
Here's what I have tried so far:
var money = [20, 20, 20, 10, 10, 5, 1];
for (var i = 0; i < money.length; i++) {
if (money[i] == money[i + 1]) {
money[i] += money[i + 1];
money.splice(money.indexOf(money[i + 1]), 1);
}
}
I would do something like this:
Count the occurrences.
Multiply the value with the occurrences.
Snippet
// Our original array.
var arr = [20, 20, 20, 10, 10, 5, 1];
// Let's have a counts object that stores the counts.
var counts = {};
// Loop through the array to get the counts.
for (var i = 0; i < arr.length; i++) {
var num = arr[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
// Have a final array.
var fin = [];
// Multiply the count with the values and push it to the final array.
for (var count in counts) {
fin.push(counts[count] * count);
}
console.log(fin);
Use Array#reduce method with a variable to store previous element.
var arr = [20, 20, 20, 10, 10, 5, 1];
// variable for storing previous element
var prev;
var res = arr.reduce(function(arr, v) {
// if element is same as previous then add
// value with last element
if (prev == v)
arr[arr.length - 1] += v;
// else push and update prev variable
else
arr.push(prev = v)
// return the array refernece
return arr;
// set initial value as empty array for result
}, [])
console.log(res);
UPDATE : If same values are not adjacent then use an object to refer the index.
var arr = [20, 20, 20, 10, 10, 5, 1];
// object for refering index
var ref = {};
var res = arr.reduce(function(arr, v) {
// check property is defined or not if
// defined update value at the index
if (ref.hasOwnProperty(v))
arr[ref[v]] += v;
else {
// else add property to object and push element
ref[v] = arr.length;
arr.push(prev = v)
}
// return array reference
return arr;
// set initial value as empty array for result
}, [])
console.log(res);
var list= [20,20,20,10,10,5,1];
var result=[];
//index of already added values
var listOfIndex=[];
for(var i=0;i<list.length;i++){
if(listOfIndex.indexOf(i)>=0){
continue;
}
var number=list[i];
for(var j=i+1;j<list.length;j++){
if(list[i]==list[j]){
number = number+list[j];
listOfIndex.push(j);//push in this list the index of the value that has been added
}
}
result.push(number);
}
console.log(result);
You could use a hash table and store the index of the result slot. This works for unsorted values as well.
var data = [20, 20, 20, 10, 10, 5, 1],
result = [];
data.forEach(function (a) {
if (!(a in this)) {
this[a] = result.push(0) - 1;
}
result[this[a]] += a;
}, Object.create(null));
console.log(result);
Another single-loop proposal using Array.prototype.reduce and a hash table that store the indices the result array being created - will handle input that is not sorted too.
See demo below:
var array = [20, 20, 20, 10, 10, 5, 1];
var result = array.reduce(function(hash){
return function(p,c) {
if(c in hash) {
p[hash[c]] += c;
} else {
// store indices in the array
hash[c] = p.push(c) - 1;
}
return p;
};
}(Object.create(null)),[]);
console.log(result);
Related
I have an array of this:
[34, 12, 56]
[100,125,19]
[30,50,69]
125 has been the highest value, it will return the index [1,1] format. Meaning 125 which is the highest value will return row 1 column 1
I was able to get the index in an array using this code
var a = [0, 21, 22, 7, 12];
var indexOfMaxValue = a.reduce((iMax, x, i, arr) => x > arr[iMax] ? i :
iMax, 0);
document.write("indexOfMaxValue = " + indexOfMaxValue); // prints
"indexOfMaxValue = 2"
Here's my approach. It flattens out all the arrays into more managable one, finds the max number and its index, and then calculates it's position using some math. Using a single array makes this calculation much easier.
const arr = [[34, 12, 56], [100,125,19], [30,50,69]];
const arr2 = [0, 21, 22, 7, 12];
function findHighest(arr) {
// Get the number of columns
const cols = arr.length;
// Flatten out the arrays
const tempArr = arr.flatMap(el => el);
// Get the max number from the array
const max = Math.max.apply(null, tempArr);
// Find its index
const indexMax = tempArr.findIndex(el => el === max);
// Find the remainder (modulo) when you divide the index
// by the number of columns
const mod = indexMax % cols;
// Return the final array output
return [Math.floor(indexMax / cols), mod];
}
console.log(findHighest(arr))
console.log(findHighest(arr2))
This will give the expected output but not sure is it good way to solve this:
var arr = [
[34, 12, 56],
[100, 125, 19],
[30, 50, 69]
];
var maxValue, maxIndex;
arr.forEach((arr1, i) => {
arr1.forEach((value, j) => {
if (i == 0 && j == 0) {
maxValue = value;
maxIndex = [i, j]
} else {
if (maxValue < value) {
maxValue = value;
maxIndex = [i, j];
}
}
});
});
console.log("Max Number Index", maxIndex);
If you mean 2d solution, try this. Should work for dynamic length arrays
This should be extendable with a new forEach for a new dimension
[100,125,19],
[30,50,69]];
maxIndex = [-1, -1];
maxElem = 0;
input.forEach(function(arr, row) {
console.error(row);
arr.forEach(function(e, col) {
if( maxElem <= e ) {
maxElem = e;
maxIndex = [row, col];
}
})
})
console.log(maxIndex)
I want to add an array add up.
Let's say there's an array of [7, 1, 21, 70]
On a an array index of 0, it would just be 7. On an array index of 1, I want it to be 7 + 1 (8). Array index 2, 7 + 1 + 21 (29). Array index 3 7 + 1 + 21 + 70 (99).
This is my current code:
var pot = {
'id': 1,
'name': ['stalin', 'hitler', 'mao', 'kim jong-il'],
'amount': [50, 10, 150, 500],
'percentages': new Array()
}
var random = Math.random()*100;
var random = String(random).split(".")[0];
console.log(random);
function potTotal(amounts) {
var sum = 0;
for (var key in amounts) {
sum += pot['amount'][key];
}
return sum;
}
function potPercentage(total, amounts) {
for (var key in amounts) {
var percentage = amounts[key] / total * 100;
var percentage = String(percentage).split(".")[0];
var percentage = Number(percentage);
pot['percentages'].push(percentage);
}
}
potPercentage(potTotal(pot['amount']), pot['amount']);
function ranging(total, percentages) {
console.log(percentages);
for(var i = 0; percentages < i; i++) {
console.log(percentages[i]);
}
}
//[7, 1, 21, 70]
ranging(random, pot['percentages']);
for (var key in pot['percentages']) {
console.log(key);
console.log(pot['percentages'][key]);
}
The results of which return:
69
[ 7, 1, 21, 70 ]
0
7
1
1
2
21
3
70
reduce is the function defined to do this kind tasks.
const arr = [7, 1, 21, 70].reduce((acc, el, i) => [...acc, (acc[i-1] || 0) + el], []);
console.log(arr);
Basically you are looking for prefix sum method, try the following:
var arr = [7, 1, 21, 70];
for(var i = 1; i < arr.length; i++){
arr[i] += arr[i-1];
}
console.log(arr);
For Reference : Prefix sum
A simple forEach() will do for you:
var arr = [7, 1, 21, 70];
arr.forEach((item, index) => {
if(index - 1 > -1){
arr[index] += arr[index-1];
}
});
console.log(arr);
Just iterate with a for loop and add the previous item
let items = [1,2,3,4,5,6,7];
for (let i = 1; i < items.length; i++)
items[i] = items[i] + items[i-1];
You could use the reduce method on the array so that you don't bother managing index while doing the computation and also reduce will return you a brand new array, with the added benefit of not destroying your original array
var originalArray = [7, 1, 21, 70];
var addUp = (acc, curr, index) => {
if (index === 0) acc.push(curr);
else acc.push(acc[index-1] + curr);
return acc;
}
var computedArray = originalArray.reduce(addUp, []);
console.log(computedArray);
I like this as this is so easy to read and does not mutate the original array.
Mapping over an array gives you access to each item in that array one by one (just like a foreach loop) but it also creates a new array containing the return value from the callback.
function addConsecutives(nums) {
let sum = 0;
return nums.map(num => sum += num)
}
console.log(addConsecutives([7, 1, 21, 70]));
I wanted to write a function to get all numbers, what is greater than all elements to its right.
Example if i have an array like this:
arr = [ 75,47,42,56,13,55];
I want a result like this [75,56,55]in a new array.
Other example if i have an array like this:
arr = [16,17,14,3,14,5,2]
I want a result like: [17,14,5,2]
What methods can i use to get this result whatever numbers i have in an array?
You can use filter the array. splice the array to get all the numbers on the right. Use every to check if all array elements are greater than the value.
let arr = [75, 47, 42, 56, 13, 55];
let result = arr.filter((v, i, a) => [...a].splice(i + 1, a.length).every(o => v > o));
console.log(result);
Doc: filter(), splice(), every()
You could simply iterate from the right side and check against the latest found greatest value.
function greaterThanRight(array) {
return array.reduceRight((r, v) => [].concat(v <= r[0] ? [] : v, r), [])
}
console.log([[75, 47, 42, 56, 13, 55], [16, 17, 14, 3, 14, 5, 2]].map(greaterThanRight).map(a => a.join(' ')));
A simple for loop where inner loop has index starting with the index value of the match having highest right value:
var arr = [ 75,47,42,56,13,55];
var res = [];
for(var i=0; i<arr.length; i++){
var highestValue = arr[i];
for(var j=i+1; j<arr.length; j++){
if(highestValue < arr[j]){
highestValue = arr[j];
i = j;
break;
}
}
res.push(highestValue);
}
console.log(res);
var arr = [75,47,42,56,13,55];
arr.sort(function(a, b) {
// a = current item in array
// b = next item in array
return b - a;
});
var newArr = arr.slice(0,3);
console.log(newArr);
Try this. Just start checking from the right.keep a variable max and update it whenever you found a new max
var arr = [ 75,47,42,56,13,55];
function findMaxRight(arr){
var res=[]
var max = Number.MIN_SAFE_INTEGER
for(var i=arr.length -1; i>=0;i--){
if(arr[i]> max){
res.unshift(arr[i])
max = arr[i]
}
}
return res
}
console.log(findMaxRight(arr));
One way to do it could be to loop your array, get the current item and get the rest of the array after the current item using slice.
Then sort the rest of the array descending and get the first entry which is the highest one.
If the current value is larger than the highest one, it is larger then all the values on the right:
let items = [75, 47, 42, 56, 13, 55];
let result = [];
items.forEach((item, index) => {
let head = items[index];
let tail = items.slice(index + 1).sort(function(a, b) {
return b - a;
});
if ((head > tail[0]) || (index === items.length - 1)) {
result.push(head);
}
});
console.log(result);
My solution to do this : find the max and if it's the current number store it, continue with the new array looping = looping.slice(1)
let arr = [75,47,42,56,13,55];
let looping = [...arr];
let finish = arr.reduce( (acc, x) => {
if(x === Math.max(...looping)) acc.push(x);
looping = looping.slice(1);
return acc;
}, [])
console.log(finish) // [75,56,55]
I created a function that will find pairs to add the two numbers that will be equal to the sum.
function findingSum(arr, sum){
var firstElement = [];
var difference = [];
var final = [];
var convertArr = arr.map(function(item){
return parseInt(item, 10);
});
for(var i = 0; i < convertArr.length; i++){
difference.push(sum - convertArr[i]); // subtracted sum from each convertArr item
if(difference[i] + convertArr[i] === sum){ // check if convertArr item was added to difference item === sum
firstElement.push(convertArr[i]); // if so add the convertArritem to the result array
}
if(firstElement[i] + convertArr[i] == sum){
final.push(firstElement[i], convertArr[i]);
}
}
return final;
}
var addNumbers = findingSum([3, 34, 4, 12, 5, 2], 9);
console.log(addNumbers); // --> [4, 5]
So what I did is that I try to get the difference of convertArr[i] and the sum and put them in a difference variable. Then I tried to see if adding difference[i] from the original array will give me the sum. If so I'll add them on firstElement array and try to add each value to the original array and finally push them along with it's addenth if the sum was attain. So when you add this two you'll get the sum.
For some reason my logic doesn't work and it does'nt push things on both firstElement and final array. Can anyone help me with this?>
You could use a hash table for visited values.
var findingSum = function (array, s) {
var a, i,
hash = Object.create(null);
for (i = 0; i < array.length; i++) {
a = array[i];
if (hash[s - a]) {
return [s - a, a];
}
if (!hash[a]) {
hash[a] = true;
}
}
};
console.log(findingSum([3, 34, 4, 12, 5, 2], 9)); // [4, 5]
I am creating a function that takes in an array of unsorted integers and returns an array of the unique integers, sorted by frequency. However, if the integers have the same frequency, they will be returned in the original order of the input array. Here is my current function:
function uniqueUnionSorted(arr) {
counter = {};
for(var i=0; i<arr.length; i++) {
if (arr[i] in counter) {
counter[arr[i]] ++;
} else {
counter[arr[i]] = 1;
}
}
sortedStrings = Object.keys(counter).sort(function(a,b) {
return counter[b] - counter[a]
});
var sortedNumbers = sortedStrings.map(Number);
return sortedNumbers;
}
So for an array like this:
arr = [1, 3, 2, 1, 5, 2, 1, 4]
the function should return:
[1,2,3,5,4]
However, my function is sorting the 5 and 4 and is returning:
[1,2,3,4,5]
Please help!
The cause of this reordering is that object properties that are numerical will come out ordered when using Object.keys().
Instead of defining counter as an object, use a Map, which will retain the insertion order:
function uniqueUnionSorted(arr) {
var counter = new Map();
for(var i=0; i<arr.length; i++) {
counter.set(arr[i], (counter.get(arr[i]) || 0) + 1);
}
// Spreading the Map will produce an array of pairs
var sortedNumbers = [...counter].sort(function(a,b) {
return b[1] - a[1]; // sort by count
}).map(a => a[0]); // only keep the values, not the counts
return sortedNumbers; // Map keys retain original type, so they remain numeric
}
arr = [1, 3, 2, 1, 5, 2, 1, 4]
console.log(uniqueUnionSorted(arr));
In counter object we can also save lowest index of each element so we can keep elements with lower index earlier in sorted array.
function uniqueUnionSorted(arr) {
counter = {};
for(var i=0; i<arr.length; i++) {
if (arr[i] in counter) {
counter[arr[i]].count ++;
} else {
counter[arr[i]] = {'count' : 1, 'index' : i}; //save lowest index of element
}
}
sortedStrings = Object.keys(counter).sort(function(a,b) {
return counter[b].count - counter[a].count || counter[a].index - counter[b].index;
});
var sortedNumbers = sortedStrings.map(Number);
return sortedNumbers;
}
console.log(uniqueUnionSorted([1, 3, 2, 1, 5, 2, 1, 4]));
https://jsfiddle.net/anLrwwfa/4/
Here's another way you could do this using a Set object, an object to store frequency, and the original array to keep the origin intact. It's a bit longer than the current popular answer but I was in the midst of writing it, so I figured I would throw my hat in the ring.
function sortArr(arr) {
let uniqueValues = new Set();
let frequencies = new Object();
//add values to set object
arr.map((val) => uniqueValues.add(val));
//get frequencies of values
for (let val of uniqueValues) {
frequencies[val] = 0;
}
arr.map((val) => frequencies[val]++);
//sort by frequency, then sort by original placement in array
let sorted_arr = Array.from(uniqueValues).sort((a, b) => {
return frequencies[a] - frequencies[b];
}).sort((a, b) => {
return (frequencies[a] === frequencies[b]) ?
Array.from(uniqueValues).indexOf(a) - Array.from(uniqueValues).indexOf(b) :
b;
});
//return array
return sorted_arr;
};
sortArr([1, 3, 2, 1, 5, 2, 1, 4]); //1,2,3,5,4
EDIT
optimized code a bit
May add an additional condition to actually sort after the original array index if the frequency is equal:
sortedStrings = Object.keys(counter).sort(function(a,b) {
return counter[b] - counter[a] || arr.indexOf(+a)-arr.indexOf(+b)
});
http://jsbin.com/zewulanozo/edit?console