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

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

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.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
&lt;!-- language: lang-js --&gt;
    const
        findTotalWithMostInstances = array =&gt; array
            .reduce((acc, { total }) =&gt; {
                acc[total] = (acc[total] || 0) + 1;
                if (acc.max &lt; 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));
&lt;!-- end snippet --&gt;

</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 }) =&gt;
    (a[total] = (a[total] || 0) + 1) &amp;&amp; a, {}))
    .sort((a, b) =&gt; 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.

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:

确定