Peter
Peter Campbell Smith

Round days and
frequent numbers

Weekly challenge 276 — 1 July 2024

Week 276: 1 Jul 2024

Task 2

Task — Maximum frequency

You are given an array of positive integers, @ints. Write a script to return the total number of elements in the given array which have the highest frequency.

Examples


Example 1
Input: @ints = (1, 2, 2, 4, 1, 5)
Output: 4
The maximum frequency is 2.
The elements 1 and 2 has the maximum frequency.

Example 2
Input: @ints = (1, 2, 3, 4, 5)
Output: 5
The maximum frequency is 1.
The elements 1, 2, 3, 4 and 5 has the maximum frequency.

Analysis

Again, the straightforward approach is good enough for even quite large numbers of numbers.

The first loop is over @ints, building $freq{$int} and establishing $max_freq.

The second loop is over keys %freq, adding up the frequencies of all the @ints which occur $max_freq times.

Trying this with a million numbers in the range 0 .. 99 took about 2 seconds on my machine to establish that 10194 occured 91 times.

Try it 

Try running the script with any input:



example: 1, 2, 3, 4, 5, 5, 6, 6, 6

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2024-07-01
use utf8;     # Week 276 - task 2 - Maximum frequency
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';

my @ints;

maximum_frequency(1, 2, 2, 4, 1, 5);
maximum_frequency(1, 2, 3, 4, 5);
maximum_frequency(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2);

push @ints, int(rand(15)) for 0 .. 99;
maximum_frequency(@ints);

sub maximum_frequency {
    
    my (@ints, $max_freq, $int, %freq, $count, $explain, $s, $they);
    
    @ints = @_;
    $max_freq = 0;
    for $int (@ints) {
        $freq{$int} ++;
        $max_freq = $freq{$int} if $freq{$int} > $max_freq;
    }
    for $int (sort keys %freq) {
        next unless $freq{$int} == $max_freq;
        $count += $freq{$int};
        $explain .= qq[$int, ];
    }

    printf(qq[\nInput:  \@ints = (%s)\n], join(', ', @ints));
    printf(qq[Output: %d integers occur with frequency %d: %s\n], $count, $max_freq, substr($explain, 0, -2));
}

Output


Input:  @ints = (1, 2, 2, 4, 1, 5)
Output: 4 integers occur with frequency 2: 1, 2

Input:  @ints = (1, 2, 3, 4, 5)
Output: 5 integers occur with frequency 1: 1, 2, 3, 4, 5

Input:  @ints = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2)
Output: 6 integers occur with frequency 3: 1, 2

Input:  @ints = (6, 6, 9, 0, 3, 13, 11, 10, 8, 7, 5, 0,
1, 2, 2, 2, 9, 11, 11, 8, 13, 11, 6, 6, 5, 6, 11, 5, 9,
14, 11, 1, 2, 6, 5, 14, 7, 13, 5, 6, 12, 5, 8, 5, 12, 14,
4, 13, 2, 10, 13, 2, 1, 5, 0, 5, 13, 9, 12, 14, 10, 5,
11, 3, 6, 3, 5, 1, 2, 1, 6, 7, 9, 11, 5, 4, 6, 10, 3, 10,
14, 7, 4, 12, 7, 1, 6, 9, 2, 13, 2, 7, 8, 7, 4, 3, 13, 3,
10, 7)
Output: 12 integers occur with frequency 12: 5

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain