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?
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.