2021年4月5日星期一

Get the key from the most frequent value in an array, or the highest key if there is a value "tie"

I have an array like the following:

2021-04-05 19:45:04 : "0001"
2021-04-05 18:45:03 : "1220"
2021-04-05 17:45:03 : "0001"
2021-04-05 16:45:03 : "1220"
2021-04-05 15:45:03 : "0001"
2021-04-05 14:45:03 : "1220"
2021-04-05 13:45:03 : "3321"

In the above scenario, among the seven elements, we have 3 unique values. 0001 exists 3 times, 1220 exists 3 times, and 3321 exists once. I need to get the most frequent value in the array, and if there is a tie, I need to get the most RECENT, most frequent value.

Currently my code returns 3321 because the date is the most recent. I would need to look at both 0001 and 1220 since they appear the most, and then ultimately I would need to select 1220 since it's the value that appears the most with the most recent date time / key.

Any suggestions would be highly appreciated!

Thank you,

https://stackoverflow.com/questions/66960745/get-the-key-from-the-most-frequent-value-in-an-array-or-the-highest-key-if-ther April 06, 2021 at 06:55AM

没有评论:

发表评论