4

I have an array of numbers like this:

$array = array(1,1,1,4,3,1);

How do I get the count of most repeated value?

3
  • 5
    possible duplication stackoverflow.com/questions/4359721/… Commented May 16, 2012 at 4:47
  • 1
    This sounds like homework. The above link is too clever for a homework assignment. No fair ;)! But "duplicate question" it is! Commented May 16, 2012 at 4:49
  • It sounds like a "homework" indeed. But you never tried to help :-(. Your input would have been helpful. @paulsm4. Commented May 16, 2012 at 5:08

10 Answers 10

15

This should work:

$count=array_count_values($array);//Counts the values in the array, returns associatve array
arsort($count);//Sort it from highest to lowest
$keys=array_keys($count);//Split the array so we can find the most occuring key
echo "The most occuring value is $keys[0][1] with $keys[0][0] occurences."
Sign up to request clarification or add additional context in comments.

2 Comments

It worked for me. I really wanted to achieve this without a loop. Thanks a lot @Peet
The code is fine but the output did not work. Correct would be: echo "The most occuring value is ".$keys[0]." with ".$count[$keys[0]]." occurences.";
4

I think array_count_values function can be useful to you. Look at this manual for details : http://php.net/manual/en/function.array-count-values.php

Comments

4

You can count the number of occurrences of values in an array with array_count_values:

$counts = array_count_values($array);

Then just do a reverse sort on the counts:

arsort($counts);

Then check the top value to get your mode.

$mode = key($counts);

Comments

3

If your array contains strings or integers only you can use array_count_values and arsort:

$array = array(1, 1, 1, 4, 3, 1);

$counts = array_count_values($array);
arsort($counts);

That would leave the most used element as the first one of $counts. You can get the count amount and value afterwards.

It is important to note that if there are several elements with the same amount of occurrences in the original array I can't say for sure which one you will get. Everything depends on the implementations of array_count_values and arsort. You will need to thoroughly test this to prevent bugs afterwards if you need any particular one, don't make any assumptions.

If you need any particular one, you'd may be better off not using arsort and write the reduction loop yourself.

$array = array(1, 1, 1, 4, 3, 1);

/* Our return values, with some useless defaults */
$max = 0;
$max_item = $array[0];

$counts = array_count_values($array);
foreach ($counts as $value => $amount) {
    if ($amount > $max) {
        $max = $amount;
        $max_item = $value;
    }
}

After the foreach loop, $max_item contains the last item that appears the most in the original array as long as array_count_values returns the elements in the order they are found (which appears to be the case based on the example of the documentation). You can get the first item to appear the most in your original array by using a non-strict comparison ($amount >= $max instead of $amount > $max).

You could even get all elements tied for the maximum amount of occurrences this way:

$array = array(1, 1, 1, 4, 3, 1);

/* Our return values */
$max = 0;
$max_items = array();

$counts = array_count_values($array);
foreach ($counts as $value => $amount) {
    if ($amount > $max) {
        $max = $amount;
        $max_items = array($value);
    } elif ($amount = $max) {
        $max_items[] = $value;
    }
}

Comments

1
$vals = array_count_values($arr);
asort($vals);
//you may need this end($vals);
echo key($vals);

I cant remember if asort sorts asc or desc by default, you can see the comment in the code.

Comments

0
<?php
$arrrand = '$arr = array(';
for ($i = 0; $i < 100000; $i++)
{
    $arrrand .= rand(0, 1000) . ',';
}
$arrrand = substr($arrrand, 0, -1);
$arrrand .= ');';
eval($arrrand);
$start1 = microtime();
$count = array_count_values($arr);
$end1 = microtime();
echo $end1 - $start1;
echo '<br>';
$start2 = microtime();
$tmparr = array();
foreach ($arr as $key => $value);
{
    if (isset($tmparr[$value]))
    {
        $tmparr[$value]++;
    } else
    {
        $tmparr[$value] = 1;
    }
}
$end2 = microtime();
echo $end2 - $start2;

Here check both solutions: 1 by array_count_values() and one by hand.

Comments

0
<?php

$input = array(1,2,2,2,8,9);

$output = array();
$maxElement = 0;

for($i=0;$i<count($input);$i++) {
    $count = 0;
    for ($j = 0; $j < count($input); $j++) {
        if ($input[$i] == $input[$j]) {
            $count++;
        }
    }
    if($count>$maxElement){
        $maxElement = $count;
        $a = $input[$i];
    }
}

echo $a.' -> '.$maxElement;

The output will be 2 -> 3

Comments

-1
$arrays = array(1, 2, 2, 2, 3, 1); // sample array

$count=array_count_values($arrays); // getting repeated value with count

asort($count); // sorting array 

$key=key($count);

echo $arrays[$key];   // get most repeated value from array

Comments

-1

String S;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter the String:  ");

    S = in.nextLine();

    int count =1;
    int max = 1;
    char maxChar=S.charAt(0);
    for(int i=1; i <S.length(); i++)
    {
        count = S.charAt(i) == S.charAt(i - 1) ? (count + 1):1;

        if(count > max)
        {
            max = count;
            maxChar = S.charAt(i);
        }
    }

    System.out.println("Longest run: "+max+", for the character "+maxChar); 

3 Comments

The question was for PHP--this Java answer is probably not helpful without more explanation of the underlying method.
syntax may vary but the logic will remain same.
absolutely. however, take a look at the other answers. by adding additional context and/or comments, they clarify the problem more.
-1

here is the solution

class TestClass {

public $keyVal;
public $keyPlace = 0;

//put your code here
public function maxused_num($array) {
    $temp = array();
    $tempval = array();
    $r = 0;
    for ($i = 0; $i <= count($array) - 1; $i++) {
        $r = 0;
        for ($j = 0; $j <= count($array) - 1; $j++) {
            if ($array[$i] == $array[$j]) {
                $r = $r + 1;
            }
        }
        $tempval[$i] = $r;
        $temp[$i] = $array[$i];
    }
    //fetch max value
    $max = 0;
    for ($i = 0; $i <= count($tempval) - 1; $i++) {
        if ($tempval[$i] > $max) {
            $max = $tempval[$i];
        }
    }
    //get value 
    for ($i = 0; $i <= count($tempval) - 1; $i++) {
        if ($tempval[$i] == $max) {
            $this->keyVal = $tempval[$i];
            $this->keyPlace = $i;
            break;
        }
    }

    // 1.place holder on array $this->keyPlace;
    // 2.number of reapeats $this->keyVal;
    return $array[$this->keyPlace];
}

}

$catch = new TestClass(); $array = array(1, 1, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 1, 2, 3, 1, 1, 2, 5, 7, 1, 9, 0, 11, 22, 1, 1, 22, 22, 35, 66, 1, 1, 1); echo $catch->maxused_num($array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.