LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to find the mode of an array of strings

Solved!
Go to solution

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?

0 Kudos
Message 1 of 8
(4,946 Views)

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.

0 Kudos
Message 2 of 8
(4,942 Views)
Solution
Accepted by topic author doradorachan

This may work also

 

apples.png

 

Nick

Message 3 of 8
(4,914 Views)

This is how i did it using some OpenG array functions, like Remove Duplicates and Filter Array.

mode.png

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 4 of 8
(4,876 Views)

@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!

 

 

Download All
Message 5 of 8
(4,838 Views)

@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.

Message 6 of 8
(4,817 Views)

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

0 Kudos
Message 7 of 8
(4,798 Views)

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.

0 Kudos
Message 8 of 8
(4,772 Views)