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
WBOYOriginal
2016-07-29 08:56:261624browse

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>
').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.

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