aboutsummaryrefslogtreecommitdiff
path: root/challenge-260/dave-jacoby/perl/ch-1.pl
blob: 82e47718957fd38d461defa80c6a7107ec4018fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ say postderef signatures state };

use DateTime;
use List::Util qw{ uniqint  };

my @examples = (

    [ 1,  2, 2, 1, 1, 3 ],
    [ 1,  2, 3 ],
    [ -2, 0, 1, -2, 1, 1, 0, 1, -2, 9 ],
);

for my $example (@examples) {
    my @ints   = $example->@*;
    my $ints   = join ',', @ints;
    my $output = unique_occurances(@ints);
    say <<"END";
    Input:  \$ints = ($ints)
    Output: $output
END
}

sub unique_occurances (@ints) {
    my %hash;
    for my $i (@ints) {
        $hash{$i}++;
    }

    # is there a more clever way to do this?
    my $before = scalar values %hash;
    my $after  = uniqint values %hash;
    return $before == $after ? 1 : 0;
}