Home > Backend Development > PHP Tutorial > Now there are N numbers, only one of which appears an odd number of times. How to find this number?

Now there are N numbers, only one of which appears an odd number of times. How to find this number?

WBOY
Release: 2016-07-29 08:56:26
Original
1626 people have browsed it

At least go through them all, traverse or something else
Then perform XOR in sequence, and the final result is that number
Example:

<code><span>// 示例数组</span><span>$arr</span> = <span>array</span>(<span>1</span>, <span>2</span>, <span>2</span>, <span>3</span>, <span>1</span>, <span>3</span>, <span>4</span>, <span>5</span>, <span>5</span>);

    <span>// 0与任何数异或都是其本身</span><span>$res</span> = <span>0</span>;
    <span>foreach</span> (<span>$arr</span><span>as</span><span>$value</span>) {
        <span>// 相同两个数异或结果为0</span><span>$res</span> = <span>$res</span> ^ <span>$value</span>;
    }

    var_dump(<span>$res</span>);</code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces that there are N numbers, and only one number appears an odd number of times. How to find this number? , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template