Home  >  Article  >  Backend Development  >  PHP method to find numbers that appear only once in an array

PHP method to find numbers that appear only once in an array

黄舟
黄舟Original
2017-10-30 09:11:081438browse

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

Question:

In addition to two numbers in an integer array , 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(&#39;22&#39;,&#39;44&#39;,&#39;66&#39;,&#39;11&#39;,&#39;11&#39;,&#39;44&#39;,&#39;33&#39;);
print_r(FindNumsAppearOnce($arr));

Output:

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

The above is the detailed content of PHP method to find numbers that appear only once in an array. 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