Is there a way to take the mode of an array of integers?

I’m representing two teams with the integers 1 (for team A) and 2 (for team B).

I have a script that is getting all actors’ team numbers (1 or 2) and throwing them into an array.

When assigning a team to a new player, the script wants to take the mode of all current team numbers (either 1 or 2) and assign new players to whichever team has fewer members.

Is there a way to take the mode of an array of integers?