playMaker

Author Topic: How to get the most frequently occurring value in an array?  (Read 1030 times)

uilk

  • Playmaker Newbie
  • *
  • Posts: 16
How to get the most frequently occurring value in an array?
« on: November 25, 2019, 05:19:05 PM »
I'm surprised there isn't a default action for this. What would be the most headache-free way of setting something like this up? Ideally if two or more values are tied for most frequently occurring I would like to return all of them.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to get the most frequently occurring value in an array?
« Reply #1 on: November 26, 2019, 01:54:11 AM »
Hi,

that's far from trivial even in pur c#.

you'll need to have a buffer that assigns a weight for each value you find in arrays over time, and then as time goes by, the weight of the most occuring values will increase, and thus you'll be able to sort values by weight and get what's most occuring this way.

I would start working on this with two arrays ( using fsmArray), or ArrayList ( using ArrayMaker), one array will represent the value, the other the weight, both array need to be maintain in sync, by their indexes. so index 3 on the first array will be the value, and index 3 on the second array will be the associated weight for that value.

Everytime your array changes or whenever you want to take a snapshot of the values to weight them, go over that array, find the index of that value in the values, if not add it, and increase the related weight by 1.

after you took several snapshots, reoccuring values will have their weight higher then others, if you want to get the highest occuring value:

1: copy the weight array into another array
2: sort that array from highest to lowest
3: get the weight value of the first item ( item 0)
4: find the index of the weight in the weights array
5: get the value from the index in the values array -> that's your highest occuring value


so indeed... not trivial at all, but doable with some patience

Bye,

 Jean

uilk

  • Playmaker Newbie
  • *
  • Posts: 16
Re: How to get the most frequently occurring value in an array?
« Reply #2 on: November 26, 2019, 08:52:04 PM »
Ok, thanks. I'll see what I can put together

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to get the most frequently occurring value in an array?
« Reply #3 on: November 27, 2019, 01:19:30 AM »
Hi,

 ok, keep us updated please, let me know if you get stuck.

Bye,

 Jean