09-05-2014 01:21 AM
Hey guys,
So I came across this little problem that I thought I could solve relatively easily, but it turned out quite messy.
I have this array of strings and I want to find the most frequent entry of that array. Let's say I have the following array.
apple
apple
kiwi
orange
apple
the mode would give me apple. I thought about converting the strings into a number and then find the mode as usual.
But if the string was too long, it seems a single number cant be used to represent it.
Any ideas on a clean way to do this?
Solved! Go to Solution.
09-05-2014 01:27 AM
Just sort the array and then loop over it counting consecutive entries and keeping track of the largest found so far.
See how far you get.
09-05-2014 02:46 AM
This may work also
Nick
09-05-2014 08:44 AM
This is how i did it using some OpenG array functions, like Remove Duplicates and Filter Array.
09-05-2014 10:44 AM - edited 09-05-2014 11:00 AM
@chembo wrote:
This may work also
You should probably link to the thread where this solution was first presented instead.
Since the question was: "I have this array of strings and I want to find the most frequent entry of that array", here's is a more tailored solution to the actual question.
Still, my more traditional approach outlined in the first answer would probably be more efficient, because it does not generate any large data structures. Try it!
09-05-2014 11:21 AM
@altenbach wrote:
@chembo wrote:
This may work also
You should probably link to the thread where this solution was first presented instead.
I definitely would do that, if I knew that this post exists. And I always do it, even if I found the info somewhere else, not on the LabVIEW forums (see example here in this thread). In this case I wrote the code myself and I am actually happy to see confirmation of my solution.
Variant attributes are very useful for searching and sorting stuff. Well maybe for many other things, but I use them mainly for lookup tables. It is very similar to hash tables in Perl.
09-05-2014 12:07 PM
Thanks guys for your help. Didn't realize variant could be used this way as well.
Altenbach, I did try the simple method of just comparing... but i couldnt make my messy code any clear.
Variant code looks much better and easier to understand
09-05-2014 04:28 PM - edited 09-05-2014 04:49 PM
Here's what I had in mind. Does not need any array allocations (LabVIEW 2013).
(Probably needs a few tweaks if you allow nonprintable inputs)
The second VI also generates the sorted output like the variant version.