简体   繁体   English

在唯一数字数组中查找重复的数字

[英]Find repeated number in an array of unique numbers

There is an array where except one number (say magic-number) all are unique. 有一个数组,其中除了一个数字(例如魔术数字)外,其他所有数字都是唯一的。 The magic-number repeats itself more than half times the size of the array. 幻数重复本身超过数组大小的一半。 eg 2, 10, 10, 10, 3. Find the magic-number without using any extra-space and without sorting it. 例如2、10、10、10、3。无需使用任何多余的空格并对其进行排序即可找到幻数。 Now is there any method to do it in O(n). 现在在O(n)中有任何方法可以做到这一点。

Check each element against its neighbors, if any are equal then you have found the number. 检查每个元素与其相邻元素是否相等,那么您已经找到了数字。 O(N) 上)

If the first test did not find the number, then you in the following situation: 如果第一个测试未找到编号,则您处于以下情况:

10,2,10,3,10.

In this case, the first number in the array is the magic number. 在这种情况下,数组中的第一个数字是幻数。 O(1) O(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM