## How to calculate biggest number of times in an array?

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

### How to calculate biggest number of times in an array?

I have this array.

01 JOCKEY-NAME-ARRAY.
02 JOCKEY-NAME OCCURS 100 TIMES PIC X(20).

It contains names of jockeys who participated in races. Some names occur several times. How do I find out which name(s) occur(s) the most number of times? I have an idea but it is too clumsy. Go through the array and move all individual names to individual arrays, then calculate and then compare the number of occurrences. I don't think I can use control break processing because the array is not sorted sequentially. Anyone has any ideas?
dexik

Posts: 28
Joined: Tue Jun 12, 2012 4:30 am
Has thanked: 0 time
Been thanked: 0 time

### Re: How to calculate biggest number of times in an array?

1. Sort the array to simplify the logic and use control breaks.

2. Create a second array and then scan through JOCKEY-NAME. If each name occurs in the second array, add 1 to a counter in that array. If the name doesn't occur in the second array, add it to the end and set the counter to 1.
Robert Sample
Global moderator

Posts: 3652
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 269 times

### Re: How to calculate biggest number of times in an array?

The "answer" always depends on the data. There are many possible "solutions", some of which will be a better fit for your data than others.

This is from a course/training, so also bear in mind specifically what you have been learning lately.

On what we know, I'd add a count to the table and instead of adding each race to the table, I'd add each jockey, setting count to one when including first time, and increasing the count for each time the jockey races. Once input is complete, go through the table to find the highest count. There are many "variations" possible with this, but that is the basic outline.
BillyBoyo
Global moderator

Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times