Home >Backend Development >PHP Tutorial >How to implement the statistical method of numbers that appear more than half in an array in PHP (code)

How to implement the statistical method of numbers that appear more than half in an array in PHP (code)

不言
不言forward
2018-10-13 14:58:262440browse

The content of this article is about how PHP implements the statistical method (code) of numbers that appear more than half of the times in an array. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. helped.

There is a number in the array that appears more than half the length of the array. Please find this number. For example, enter an array {1,2,3,2,2,2,5,4,2} with a length of 9. Since the number 2 appears 5 times in the array, which is more than half the length of the array, 2 is output. If it does not exist, output 0.

Two ways:

1. Define a new array arr, traverse the array and assign values ​​to arr, arr[element] = the number of occurrences
2. Sort arr, take the first key and value, key is the target element, and value is the number of occurrences. After verification, it will return
3. The time complexity is O(n) and a new array will be created in space

1. Define variables e represents the element that appears the most. The variable count is used to determine the number of occurrences. Use
2. Traverse the array and compare the current element with e. The same count, different count--, when the count is 0, the current element covers e
3. Traverse the array to verify whether e appears more than half the time
4. Time complexity O(n) Space complexity O(n)

e,count=1
for i=1;i<arr.length;i++
    if arr[i]==e
        count++
    else
        count--
    if count==0
        e=arr[i]
        count=1
count=0
for i=0;i<arr.length;i++
    if arr[i]==e
        count++
if count*2>arr.length
    return e
<?php
$arr=array(1,2,3,2,2,2,5,4,2);
$e=MoreThanHalfNum_Solution($arr);
var_dump($e);

function MoreThanHalfNum_Solution($numbers){
        $arr=$numbers;
        $e=$arr[0];
        $count=1;
        $length=count($arr);
        for($i=1;$i<$length;$i++){
                if($arr[$i]==$e){
                        $count++;
                }else{
                        $count--;
                }   

                if($count==0){
                        $e=$arr[$i];
                        $count=1;
                }   
        }   
        $count=0;
        for($i=0;$i<$length;$i++){
                if($arr[$i]==$e){
                        $count++;
                }   
        }   
        if($count*2>$length){
                return $e; 
        }   
        return 0;

}

The above is the detailed content of How to implement the statistical method of numbers that appear more than half in an array in PHP (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete