1 #!/usr/bin/perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-199/#TASK1
 3 #
 4 # You are given a list of integers, @list.
 5 #
 6 # Write a script to find the total count of Good Pairs.
 7 #
 8 ## A pair (i, j) is called good if list[i] == list[j] and i < j.
 9 #
10 ####################
11 #
12 # solution:
13 #
14 # The solution to this problem is quite simple. Walk from the first element to
15 # the last with i, then walk from i to the last element with j, and take a note
16 # of the pair if list[i] == list[j]. Nothing too wild.
17 
18 use strict;
19 use warnings;
20 
21 # some examples
22 my @lists = (
23    [1,2,3,1,1,3],
24    [1,2,3],
25    [1,1,1,1],
26    [],
27    [1,2,3,4,1,2,3,1,2,1]
28 );
29 
30 foreach my $list (@lists) {
31    find_good_pairs(@$list);
32 }
33 
34 
35 sub find_good_pairs {
36    my @list = @_;
37    my $count = 0;
38    foreach my $i (0..$#list) {
39       foreach my $j ($i+1..$#list) {
40          $count++ if $list[$i] == $list[$j];
41       }
42    }
43    print "[" . join(",",@list) . "] returns $count\n";
44 }