How to limit an Array and get the item that have the most in it?

For example, I want to save the most played game type in an Array with limit of 10 itens that it saves
It can be in Integer, like game 1, 2, 3, 4
And return the number it have the most in it
(If you have an easier way to save and get the most played game type, I’d be grateful)

you might be trying to over optimize the situation I will address the question first with a few options, but you are really asking about 2 different things.

1)create a struct which has at the very least your GameType identifier, and an TimesPlayed integer (you can add some other stuff for analytics, and what not).

create an AddToArray() function
that does a ForEach_with_break on the Array
-if the GameTypeID matches the one attempting to be added just increase the TimesPlayed and break out of the loop (if this is a function you would call return instead)
-out of the completed on the loop check the Array.Length() < 10 then add a new instance of the struct with the GameType Identifier and TimesPlayed = 1
-if the Array has 10 then do nothing.

2)use a Map where the GameType is the Key (you can have the Element be a single dataType, or a struct)
Map has a built in getByKey() which will attempt to return the Element
-if it fails then check the Map.Length() < 10 and add the new element.


the issues with any direct approach like this is it will be first come first serve, if the 11th+ entry that would be added would become a higher count then the first 10 it will never be added, so it will never be tracked.
so I would instead suggest to have 2 arrays really one with near permanent lifespan, and the other created temporarily.
the first Array/Map would hold the data of every GameType (this could even be held on a server to save local system resources).
then on request traverse the AllGameTypesData Array/Map and build an array of the 10 with the highest TimesPlayed. and return that.

by storing the data on all GameTypes it might be meaningful to see which ones are not being played as often to maybe address “why is this one not as popular…”

1 Like

Thank you, first solution worked for me!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.