Example of an implementation of a digital algorithm in which PHP finds more than half the length of an array

Source: Internet
Author: User
The example in this paper describes the implementation of the PHP algorithm to find the number of occurrences in the array more than half the length of the array. Share to everyone for your reference, as follows:

The <?php* algorithm requires that a number in the array appear more than half the length of the array to find the number. * * Algorithm Analysis: We need to calculate the number of occurrences of each number in the array. In PHP we can use the In_array function * To determine whether an element appears in an array. For example, the array contains three elements, we have to determine whether 1 exists * can use In_array (1, $array) to judge, but this can only Judge 1 appears once, because for an array containing an array * element 1,2,3,1, the function can only judge the existence of 1 in the array, Instead of giving a specific number of occurrences. * Because we can only give it one parameter at a time, after we have determined that the first 1 exists, the function returns, in order to be able to continue * judging after a 1, we need to loop and let the function execute multiple times. * * To do this, we need a copy of the original array. We loop through the original array and then determine whether each element exists in the copy array. * Consider arrays Array (1,2,3,1), and copy Array (1,2,3,1). They are exactly the same, and when we loop the first array, we first determine that the first 1 is present, the counter is incremented, and when we loop to the second 1, the counter is self-incremented. This allows us to accurately determine the number of occurrences of each element of the array. * * We store numbers and corresponding occurrences in the array using key pairs. For example, array (' 1 ' =>2), indicating that the number 1 appears * 2 times. * * The following code gives a specific implementation process. */$array =array (1,1,3,1,1,14,1,1,4,6,7,1,1,21,33);//Create the original array $temp= $array;//Get a copy of the original array $result=array ();// Initialize the counter array foreach ($array as $value)//Loop original Array {if (In_array ($value, $temp))//Determine if the number exists in the copy array {if (Isset ($result [$value]))//      Determine if the number has been counted {$result [$value]= $result [$value]+1;      If it has already occurred, the counter is incremented} else {$result [$value]=1; If not, the counter initializes 1}} $len =count ($array);//calculate array length foreach ($result as $key + $value)//Loop meterArray of numbers, where the $key represents a number, $value indicates the number of occurrences {if ($value > ($len/2))//If a number has occurred more than half the length of the array {echo ' array in which there are elements '. $len. ' <br> '; echo $key. ' appeared '. $value. ', over half the length of the array '.    ($len/2);    Break Only one number in the array can occur more than half the length of the array}}?>

Operation Result:

The total elements in the array 15 1 appear 8 times, more than half the length of the array 7.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.