tags:

views:

271

answers:

4

Lets say I have a simple 1D array with 10-20 entries. Some will be duplicate, How would I find out which entry is used the most? like..

$code = Array("test" , "cat" , "test" , "this", "that", "then");

How would I show "test" as the most used entry?

+9  A: 
$code = Array("test" , "cat" , "test" , "this", "that", "then");

function array_most_common($input) { 
  $counted = array_count_values($input); 
  arsort($counted); 
  return(key($counted));     
}

echo '<pre>';
print_r(array_most_common($code));
Ivo Sabev
Sorting the entire array just to find the maximum seems a bit wasteful.
Thomas
@Thomas is there an obvious, faster alternative seeing as the array does not come pre-sorted? Not arguing, just asking.
Pekka
@Thomas Sort will be faster than linear loop over the array. If you wish you can always remove the arsort() and do a for loop to determine which one is with highest count.
Ivo Sabev
@Pekka: Sort is O(_n_ log _n_), and you should be able to determine the maximum in O(_n_) time. But maybe PHP's implementation of `arsort` versus rolling your own `for` loop makes the sort come out faster in practice; I don't know.
Thomas
+4  A: 

Use array_count_values

Pekka
A: 

First keep in mind that those are values, not keys. PHP implicitly assigns numeric keys to arrays that don't have explicit keys. ie, "cat" is actually 1=>cat

$c = array_count_values($code); will return an array of all the items and how many times they occurred. You can max($c) to get the number.

You can loop through the array and set up a counter or something if you want to be able to get just the name of the value and how many times it occurred.

rkulla
+2  A: 

You can get a count of the number of occurrences of each value by using array_count_values.

$code = array("test" , "cat" , "cat", "test" , "this", "that", "then");
$counts = array_count_values($code);
var_dump($counts);
/*
array(5) {
  ["test"]=>
  int(2)
  ["cat"]=>
  int(2)
  ["this"]=>
  int(1)
  ["that"]=>
  int(1)
  ["then"]=>
  int(1)
}
*/

To get the most frequently occurring value, you can call max on the array and then access the (first) value with array_search.

$code = array("test" , "cat" , "cat", "test" , "this", "that", "then");
$counts = array_count_values($code);
$max = max($counts);
$top = array_search($max, $counts);
var_dump($max, $top);
/*
int(2)
string(4) "test"
*/

If you wish to cater for multiple most-frequent values, then something like the following would work:

$code = array("test" , "cat" , "cat", "test" , "this", "that", "then");
$counts = array_count_values($code);
$max = max($counts);
$top = array_keys($counts, $max);
var_dump($max, $top);
/*
int(2)
array(2) {
  [0]=>
  string(4) "test"
  [1]=>
  string(3) "cat"
}
*/
salathe