Getting the most duplicate values ​​in an array

I have an array of numbers like this:

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

How to get the number of most duplicate values?

+3
source share
9 answers

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."
+12
source

I think the array_count_values ​​function might be useful to you. See this guide for more details: http://php.net/manual/en/function.array-count-values.php

+4
source

, array_count_values arsort:

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

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

$counts. .

, , , . array_count_values arsort. , , - , .

- , arsort .

$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;
    }
}

foreach $max_item , , array_count_values , (, , ). , , ($amount >= $max $amount > $max).

, :

$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;
    }
}
+3
$vals = array_count_values($arr);
asort($vals);
//you may need this end($vals);
echo key($vals);

, asort asc desc , .

+1

array_count_values:

$counts = array_count_values($array);

:

arsort($counts);

Then check the upper value to get your mode.

$mode = key($counts);
+1
source
<?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 on array_count_values() and one after the other.

0
source
$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
-1
source

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); 
-1
source

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, 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);

-1
source

All Articles