算法找到以阵列奇数项(没有对) [英] Algorithm to find odd item ( with no pairs ) in an array

查看:117
本文介绍了算法找到以阵列奇数项(没有对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  找到一个单一的数字在列表

什么是给定整数数组好的算法,所有,但其中一个出现的偶数次,找到一个整数出现的次数为奇数

What to be a good algorithm given an array of integers, all but one of which appears an even number of times, find the one integer which appears an odd number of times.

也许沿东西折半查找的行?

Perhaps something along the line of binary search ?

总和的N / 2的大小2小数组的所有元素,比较,递归找出?

Sum all elements of 2 small arrays of n/2 size , compare , recursively find out ?

请一些人给的一些想法。

Please some one give some ideas .

我在编辑一个问题:

难道这XOR算法,实际上假定{1,1,4,4,7,7,5,8,8,9,9}?

Is this XOR algorithm actually assuming {1,1,4,4,7,7,5,8,8,9,9} ?

我的输入可以randmon太 - {1,} 4,1,8,9,5,4,5,9,8 - 因此,逻辑的变化?

My input can be randmon too - { 1 ,4,1,8,9,5,4,5,9,8} - So the logic changes ?

推荐答案

异或所有的数组元素。其结果将是,重复的次数奇数元素

XOR all the array elements. The result will be the element that repeats odd number of times.

这篇关于算法找到以阵列奇数项(没有对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆