英文:
Most efficient way to calculate the highest value based on it's instances in array of objects
问题
Here's the translated code:
假设我有一个对象数组如下:
```javascript
const objects = [{ total: 2 }, { total: 5 }, { total: 1 }, { total: 8 },{ total: 2 },{ total: 3 },{ total: 8 },{ total: 1 },{ total: 2 }];
如何以最有效的方式计算出现最多次数的总和?在我们的案例中,答案是2,因为它比其他所有总数出现次数多。
function findTotalWithMostInstances(arr) {
const frequency = arr.reduce((acc, obj) => {
const total = obj.total;
acc[total] = (acc[total] || 0) + 1;
return acc;
}, {});
let totalWithMostInstances;
let highestFrequency = 0;
for (const total in frequency) {
if (frequency[total] > highestFrequency) {
totalWithMostInstances = total;
highestFrequency = frequency[total];
}
}
return parseInt(totalWithMostInstances);
}
让它更加高效。
如果您需要进一步的解释或帮助,请告诉我。
<details>
<summary>英文:</summary>
Assume I have an array of objects the following:
const objects = [{ total: 2 }, { total: 5 }, { total: 1 }, { total: 8 },{ total: 2 },{ total: 3 },{ total: 8 },{ total: 1 },{ total: 2 }];
What would be the best way to calculate what is the total with most instances? it our case it would be 2 as it appears more than all the rest.
function findTotalWithMostInstances(arr) {
const frequency = arr.reduce((acc, obj) => {
const total = obj.total;
acc[total] = (acc[total] || 0) + 1;
return acc;
}, {});
let totalWithMostInstances;
let highestFrequency = 0;
for (const total in frequency) {
if (frequency[total] > highestFrequency) {
totalWithMostInstances = total;
highestFrequency = frequency[total];
}
}
return parseInt(totalWithMostInstances);
}
Make it more efficient.
</details>
# 答案1
**得分**: 3
以下是您要翻译的内容:
You could reduce with a single loop and an object with additional max and values properties.
In each iteration increment the has and check if the count is greater than `max`, then get a new array for `values` with `total` as single item and increment `max`. If `max` and count is equal, just add `total` to `values`.
Finally return only `values` property.
<details>
<summary>英文:</summary>
You could reduce with a single loop and an object with additional max and values properties.
In each iteration increment the has and check if the count is greater than `max`, then get a new array for `values` with `total` as single item and increment `max`. If `max` and count is equal, just add `total` to `values`.
Finally return only `values` property.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
findTotalWithMostInstances = array => array
.reduce((acc, { total }) => {
acc[total] = (acc[total] || 0) + 1;
if (acc.max < acc[total]) {
acc.values = [total];
acc.max++;
} else if (acc.max === acc[total]) {
acc.values.push(total);
}
return acc;
}, { max: 0 })
.values,
objects = [{ total: 2 }, { total: 5 }, { total: 1 }, { total: 8 },{ total: 2 },{ total: 3 },{ total: 8 },{ total: 1 },{ total: 2 }];
console.log(findTotalWithMostInstances(objects));
<!-- end snippet -->
</details>
# 答案2
**得分**: 2
以下是翻译好的部分:
可以有多个出现相同次数的总数。这将为您提供一个按实例从多到少排序的列表:
```javascript
const objects = [
{ total: 2 }, { total: 5 },
{ total: 1 }, { total: 8 },
{ total: 2 }, { total: 3 },
{ total: 8 }, { total: 1 },
{ total: 2 }
];
console.log(
Object.entries(objects.reduce((a, { total }) =>
(a[total] = (a[total] || 0) + 1) && a, {}))
.sort((a, b) => b[1] - a[1])
);
解释: 使用减少为每个总数值工作的对象作为哈希表,其中包含每个总数值的计数器。使用Object.entries将对象转换为数组的数组。根据出现次数对数组进行排序。
英文:
There can be more than one total that occurs the same number of times. This would give you a list sorted from most instances to least:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const objects = [
{ total: 2 }, { total: 5 },
{ total: 1 }, { total: 8 },
{ total: 2 }, { total: 3 },
{ total: 8 }, { total: 1 },
{ total: 2 }
];
console.log(
Object.entries(objects.reduce((a, { total }) =>
(a[total] = (a[total] || 0) + 1) && a, {}))
.sort((a, b) => b[1] - a[1])
);
<!-- end snippet -->
Explanation: Reducing into an object working as a hash table with counters for each value of total. Using Object.entries to convert the object into an array of arrays. Sorting the array based on occurences.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论