424,303 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

Counting Duplicate Array Elements

P: 1
i want to count the no of variables that are same in an array please help me out
Mar 15 '07 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
one way to do it:

Expand|Select|Wrap|Line Numbers
  1. my @array = qw(foo bar foo bar baz foo baz bar foo);
  2. my %counts = ();
  3. for (@array) {
  4.    $counts{$_}++;
  5. }
  6. foreach my $keys (keys %counts) {
  7.    print "$keys = $counts{$keys}\n";
  8. }
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. ;)
Mar 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.