12-12-2011 03:48 PM
Hello,
I'm looking for an elegant way of finding the most common value in an array of enums
My enum being:
0 - Close
1 - Open
2 - Undefined
For instance, if my array contains:
Close, Close, Open, Close, Close, Open.
The most common value would be "Close"
I have created a very customized VI that allows me to obtain the desired result, but I'm really not proud of doing it this way, simply because I would have to modify it if I were to add a new value to my enum...
If someone can share some ideas to enlighten me, I would REALLY appreciate it.
Thanks in advance!
Solved! Go to Solution.
12-12-2011 03:56 PM
Can you simply ask for the median of the array?
12-12-2011 04:00 PM
If you have the full version, I would ignore the coercion dots and use the Mode.vi.
12-12-2011 05:04 PM
12-12-2011 05:19 PM
There is always the brute force method:
12-12-2011 05:34 PM
If the array is not too large where a copy would be an issue you could always sort the array and use a search to look at the index where each one starts. Your loop would probably require fewer iterations than the brute force method.
12-13-2011 07:47 AM - edited 12-13-2011 07:49 AM
@Mark
The array could be very very large sometimes. I'll run some benchmarks to check for performance issues, but your method is pretty darn good nonetheless, thank you 🙂
@Darin
Brute force or not, it is far better from what I've created, thank you!
12-13-2011 11:38 AM
I am not sure I would go the sorting route, it could in priniciple save a single iteration since you know 0 is the first starting point, but you then have to deal with determining lengths, and dealing with missing values which return -1, etc.
If you really expect very large arrays, I would shift from my previous version which is meant to be quick to write to another simple method of counting.
12-13-2011 04:14 PM
Here is the variant using the search method. This method will run N-1 times where N is the number of ENUM elements. Yes, it is a bit more complex than the brute force method but it would execute fairly quickly. The sort would probably be the time consuming task.
12-13-2011 04:38 PM
Follow up. At face value it seems like the search method would be faster and it is provided you ignore the sort time. The overall performance is better with the with the brute force method. A test of an array with 1000000 elements showed the brute force method took 9 ms. The search method took 98 ms. However 94 ms of that time was the sort.