出现奇数次的数字_查找出现奇数次的数字

出现奇数次的数字

Problem statement

问题陈述

Given an array of positive numbers where all the numbers occur even number of times except only one number which occurs an odd number of times. We need to find the special number (number occurring an odd number of times in the array) using minimum time complexity and space.

给定一个正数数组,其中所有数字出现偶数次,只有一个数字出现奇数次。 我们需要使用最小的时间复杂度和空间来查找特殊数字(该数字在数组中出现奇数次)。

Solution

  1. There’s, of course, a brute force solution existing where you can just take each element and scan the rest of the array for finding an odd number of occurrence. But such brute force solution is not acceptable since it needs O(n2) time complexity.

    当然,有一个蛮力解决方案,您可以只获取每个元素并扫描数组的其余部分以查找奇数次出现的情况。 但是这种蛮力解决方案是不可接受的,因为它需要O(n 2 )时间复杂度。

  2. Hashtable can be considered as another technique for such problems. We can simply keep the counter for each element entered to the hash table and the element with an odd counter is the special element to be found. Though the time complexity is O(n), the creation of hashtable needs additional space.

    哈希表可以被视为解决此类问题的另一种技术。 我们可以简单地为输入到哈希表中的每个元素保留计数器,而具有奇数计数器的元素是要找到的特殊元素。 尽管时间复杂度为O(n) ,但哈希表的创建需要额外的空间。

  3. Needless to say, the hash table could have been taken as our efficient solution for this problem since lower time complexity. But there exists even simple and efficient solution which can be done by bitwise XORing operation. The algorithm is very simple:

    不用说,由于较低的时间复杂度,哈希表可以作为我们解决该问题的有效方法。 但是,甚至存在可以通过按位XORing操作完成的简单有效的解决方案。 该算法非常简单:

    • XOR operation for all the elements and the output will be the special number which has an odd number of occurrence.XOR操作,输出将是出现奇数的特殊数字。
    • A XOR A=0XOR A = 0 XORing for all elements as it occurs for an odd number of times.异或 ,因为它发生了奇数次。

Time complexity: O(n)

时间复杂度: O(n)

Space Complexity: O(1) (constant)

空间复杂度: O(1)(恒定)

C code for implementation

实现的C代码

#include<stdio.h>
#include<stdlib.h>

int specialNo(int a[], int n){
	int x=0;

	for(int i=0;i<n;i++){
		//xoring all elements outputs the special number
		x^=a[i];
	}

	//return the special number
	return x;
}

int main(){
	int x,count=0;

	// array of positive no initialized to -1 for now
	int a[20]={-1}; 

	printf("enter positive numbers  ending with -1\n");
	scanf("%d",&x);

	// first no
	a[0]=x;           
	while(x!=-1){
		scanf("%d",&x);
		//from second no onwards
		a[count+1]=x;  
		count++;
	}

	// function to check duplicate exists or not
	printf("\nthe special number occuring odd no of times is %d\n",specialNo(a,count)); 

	return 0;
}

Output

输出量

enter positive numbers  ending with -1
1
2
3
2
4
1
4
3
5
3
5
-1

the special number occuring odd no of times is 3 


翻译自: https://www.includehelp.com/algorithms/find-the-number-occurring-an-odd-number-of-times.aspx

出现奇数次的数字

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值