Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.
Example:
I/P = [1, 2, 3, 2, 3, 1, 3]
O/P = 3
Algorithm:
Do bitwise XOR of all the elements. Finally we get the number which has odd occurrences.
Program:
本文介绍了一种在数组中找到出现次数为奇数次的数字的方法。通过使用位运算中的异或操作,可以在O(n)的时间复杂度内完成任务,并且只需要常数级别的额外空间。文章还提供了一个C语言实现的例子。
3971

被折叠的 条评论
为什么被折叠?



