The Weekly Challenge - 308

TASK #1: Count Common
You are given two array of strings, @str1 and @str2.

Write a script to return the count of common strings in both arrays.

#!/usr/bin/perl
use strict;
use warnings;

# Count pairwise equal elements between two arrays (passed by reference) 
sub count_common {
	# Array references to the two input arrays 
    my ($aref1, $aref2) = @_;   

    # Dereference both arrays into local copys 
    my @arr1 = @$aref1;          
    my @arr2 = @$aref2;

    # Initialize counter for matches 
    my $match = 0;

    # Iterate over every element in the first array 
    for my $i (0 .. $#arr1) {

        # Iterate over every element in the second array 
        for my $j (0 .. $#arr2) {

            # Increment counter if elements are equal 
            $match++ if $arr1[$i] eq $arr2[$j];
        }
    }

    # Return total number of matches 
    return $match;               
}

# Tests

my (@str1, @str2);

@str1 = ("perl", "weekly", "challenge");
@str2 = ("raku", "weekly", "challenge");
printf "%d\n", count_common(\@str1, \@str2); # Output: 2 

@str1 = ("perl", "raku", "python");
@str2 = ("python", "java");
printf "%d\n", count_common(\@str1, \@str2); # Output: 1 

@str1 = ("guest", "contribution");
@str2 = ("fun", "weekly", "challenge");
printf "%d\n", count_common(\@str1, \@str2); # Output: 0