*Find the Number Occurring Odd Number of Times

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

Simple Solution is to run two nested loops. The outer loop picks all elements one by one and inner loop counts number of occurrences of the element picked by outer loop. Time complexity of this solution is O(n2).

Better Solution is to use Hashing. Use array elements as key and their counts as value. Create an empty hash table. One by one traverse the given array elements and store counts. Time complexity of this solution is O(n). But it requires extra space for hashing.

The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring element. Please note that XOR of two elements is 0 if both elements are same and XOR of a number x with 0 is x.

Below are implementations of this best approach.

Program:

//in c++

#include <stdio.h>

int getOddOccurrence(int ar[], int ar_size)
{
     int i;
     int res = 0;
     for (i=0; i < ar_size; i++)
        res = res ^ ar[i];

     return res;
}

/* Diver function to test above function */
int main()
{
     int ar[] = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
     int n = sizeof(ar)/sizeof(ar[0]);
     printf("%d", getOddOccurrence(ar, n));
     return 0;
}
时间: 2024-08-27 05:32:21

*Find the Number Occurring Odd Number of Times的相关文章

Find n’th number in a number system with only 3 and 4

这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in the number system. First few numbers in the number system are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343, 3344, 343

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

python 利用位移法将ip转为number以及将number转为ip

简介: 使用位移法将ip转为number型以及将number型转为ip,使用语言为python2.7 #!/usr/bin/env python # coding:utf-8 def ip2num(ip): ip = [int(x) for x in ip.split('.')] return ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3] print ip2num('10.101.101.13') #174417165 d

Single Number和Single Number II

1 Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解析: a ^ a = 0,a ^ 0 = a 所以对所

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Number()和new Number()的区别以及造个简单的轮子

看JavaScript文档的时候注意到了这种用法 var n1 = Number(123); , 冒出的第一个疑问就是和 var n2 = new Number(123); 有什么区别呢? 首先用typeof做下探测, n1是number而n2是object, 他们的本质区别就是type不同. 那么有趣的问题来了, Number内部肯定知道是怎么调用的它. 假设在没有Number的情况下, 如果我们要实现个类似的类应该怎么做呢? 最先想到的就是根据caller来区分, 但在实验的过程中发现两个问

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

leetcode 136. Single Number &amp;&amp; 137. Single Number II

Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 异或运算应用: 异或运算满足交换律和结合律, 通过交换元素(如插入排序等)可以使得相同元