PHP: how to find a number that appears only once in an array [find a specific element]

Source: Internet
Author: User

PHP: how to find a number that appears only once in an array [find a specific element]

This example describes how to find only one digit in an array in PHP. We will share this with you for your reference. The details are as follows:

Problem:

In an integer array, all numbers except two appear twice. Write a program to find the numbers that appear only once.

The implementation code is as follows:

<? Phpfunction FindNumsAppearOnce ($ array) {// write code here // return list, such as [a, B], here, AB is the two digits that appear once $ 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)

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.