Counting Duplicate Array Elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdulhameed
    New Member
    • Mar 2007
    • 1

    Counting Duplicate Array Elements

    i want to count the no of variables that are same in an array please help me out
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    one way to do it:

    Code:
    my @array = qw(foo bar foo bar baz foo baz bar foo);
    my %counts = ();
    for (@array) {
       $counts{$_}++;
    }
    foreach my $keys (keys %counts) {
       print "$keys = $counts{$keys}\n";
    }
    next time please post the code you have been working with, it might have been very close to working or not, but at least it shows effort on your part to solve the problem. ;)

    Comment

    Working...