# Robson ▸ Little Red Book ▸ Occurence count

## Chapter 7 - Question 9

Process a sequence of integer values to determine which values occur in the sequence and how many times each occurs.

### Solution 1

`<?` `    // generate a random number of random numbers` `    for (\$n = 0; \$n < mt_rand(5, 20); \$n++)` `        \$sequence[] = mt_rand(1, 10);` `        ` `    // count up the number of times each value occurs` `    // place the results in an array` `    \$occurence = array_count_values(\$sequence);` `    // sort and maintain keys` `    ksort(\$occurence);` `    ` `    // show the sequence and start a list` `    echo 'Sequence: ' . implode(', ', \$sequence) . '<ul>';` `    ` `    // loop through each occurence` `    foreach (\$occurence as \$value => \$frequency) ` `        // show the value and the amount of times it's found in the sequence` `        echo '<li>' . \$value . ' occurs ' . \$frequency . ' time(s)</li>';` `        ` `    // end the list` `    echo '</ul>';    ` `?>`

Which produces:

Sequence: 2, 7, 8, 2, 7, 10, 10, 8
• 2 occurs 2 time(s)
• 7 occurs 2 time(s)
• 8 occurs 2 time(s)
• 10 occurs 2 time(s)

## Log

• May 2, 2005 - Added solution 1.
© 2004-20 robson | cc unless stated