I have a string array with values in it (duh...).
Is there an easy way of getting the entry which occurs the most? Something like
values[37].getMostOften();
Cheers :)
A simple solution is to run two loops and count occurrences of every word. Time complexity of this solution is O(n * n * MAX_WORD_LEN).
A simple solution is to run two loops. The outer loop picks all elements one by one. The inner loop finds the frequency of the picked element and compares it with the maximum so far.
Make use of Python Counter which returns count of each element in the list. Thus, we simply find the most common element by using most_common() method.
Steps to find the most frequency value in a NumPy array: Create a NumPy array. Apply bincount() method of NumPy to get the count of occurrences of each element in the array. The n, apply argmax() method to get the value having a maximum number of occurrences(frequency).
You can use GroupBy
:
var mostCommonValue = values.GroupBy(v => v)
.OrderByDescending(g => g.Count())
.Select(g => g.Key)
.FirstOrDefault();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With