最有效的方法是根据对象数组中的实例来计算最高值。

huangapple go评论102阅读模式
英文:

Most efficient way to calculate the highest value based on it's instances in array of objects

问题

Here's the translated code:

  1. 假设我有一个对象数组如下
  2. ```javascript
  3. const objects = [{ total: 2 }, { total: 5 }, { total: 1 }, { total: 8 },{ total: 2 },{ total: 3 },{ total: 8 },{ total: 1 },{ total: 2 }];

如何以最有效的方式计算出现最多次数的总和?在我们的案例中,答案是2,因为它比其他所有总数出现次数多。

  1. function findTotalWithMostInstances(arr) {
  2. const frequency = arr.reduce((acc, obj) => {
  3. const total = obj.total;
  4. acc[total] = (acc[total] || 0) + 1;
  5. return acc;
  6. }, {});
  7. let totalWithMostInstances;
  8. let highestFrequency = 0;
  9. for (const total in frequency) {
  10. if (frequency[total] > highestFrequency) {
  11. totalWithMostInstances = total;
  12. highestFrequency = frequency[total];
  13. }
  14. }
  15. return parseInt(totalWithMostInstances);
  16. }

让它更加高效。

  1. 如果您需要进一步的解释或帮助,请告诉我。
  2. <details>
  3. <summary>英文:</summary>
  4. 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 }];

  1. 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);
}

  1. Make it more efficient.
  2. </details>
  3. # 答案1
  4. **得分**: 3
  5. 以下是您要翻译的内容:
  6. You could reduce with a single loop and an object with additional max and values properties.
  7. 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`.
  8. Finally return only `values` property.
  9. <details>
  10. <summary>英文:</summary>
  11. You could reduce with a single loop and an object with additional max and values properties.
  12. 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`.
  13. Finally return only `values` property.
  14. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  15. &lt;!-- language: lang-js --&gt;
  16. const
  17. findTotalWithMostInstances = array =&gt; array
  18. .reduce((acc, { total }) =&gt; {
  19. acc[total] = (acc[total] || 0) + 1;
  20. if (acc.max &lt; acc[total]) {
  21. acc.values = [total];
  22. acc.max++;
  23. } else if (acc.max === acc[total]) {
  24. acc.values.push(total);
  25. }
  26. return acc;
  27. }, { max: 0 })
  28. .values,
  29. objects = [{ total: 2 }, { total: 5 }, { total: 1 }, { total: 8 },{ total: 2 },{ total: 3 },{ total: 8 },{ total: 1 },{ total: 2 }];
  30. console.log(findTotalWithMostInstances(objects));
  31. &lt;!-- end snippet --&gt;
  32. </details>
  33. # 答案2
  34. **得分**: 2
  35. 以下是翻译好的部分:
  36. 可以有多个出现相同次数的总数。这将为您提供一个按实例从多到少排序的列表:
  37. ```javascript
  38. const objects = [
  39. { total: 2 }, { total: 5 },
  40. { total: 1 }, { total: 8 },
  41. { total: 2 }, { total: 3 },
  42. { total: 8 }, { total: 1 },
  43. { total: 2 }
  44. ];
  45. console.log(
  46. Object.entries(objects.reduce((a, { total }) =>
  47. (a[total] = (a[total] || 0) + 1) && a, {}))
  48. .sort((a, b) => b[1] - a[1])
  49. );

解释: 使用减少为每个总数值工作的对象作为哈希表,其中包含每个总数值的计数器。使用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 -->

  1. const objects = [
  2. { total: 2 }, { total: 5 },
  3. { total: 1 }, { total: 8 },
  4. { total: 2 }, { total: 3 },
  5. { total: 8 }, { total: 1 },
  6. { total: 2 }
  7. ];
  8. console.log(
  9. Object.entries(objects.reduce((a, { total }) =&gt;
  10. (a[total] = (a[total] || 0) + 1) &amp;&amp; a, {}))
  11. .sort((a, b) =&gt; b[1] - a[1])
  12. );

<!-- 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.

huangapple
  • 本文由 发表于 2023年5月7日 03:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定