Home  >  Article  >  Backend Development  >  PHP find array without repeating numbers

PHP find array without repeating numbers

php中世界最好的语言
php中世界最好的语言Original
2018-04-11 10:14:591572browse

This time I will bring you PHP searchArrayDoes not repeat numbers. What are the Notes for PHP to search for arrays that do not repeat numbers? The following is a practical case, let’s take a look.

The example in this article describes the implementation method of PHP searching for numbers that appear only once in an array. Share it with everyone for your reference, the details are as follows:

question:

Except for two numbers in an integer array, all other numbers appear twice. Please write a program to find these two numbers that only appear once.

The implementation code is as follows:

<?php
function FindNumsAppearOnce($array)
{
  // write code here
  // return list, 比如[a,b],其中ab是出现一次的两个数字
  $count = array_count_values($array);
  foreach($count as $k=>$v) {
    if($v == 1) {
      $new_arr[] = $k;
    }
  }
  return $new_arr;
}
$arr=array('22','44','66','11','11','44','33');
print_r(FindNumsAppearOnce($arr));

Output:

Array
(
  [0] => 22
  [1] => 66
  [2] => 33
)

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the use of callback functions in PHP

How to use the file_put_contents function in PHP

The above is the detailed content of PHP find array without repeating numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn