博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Find the two non-repeating elements in an array of repeating elements
阅读量:4151 次
发布时间:2019-05-25

本文共 2213 字,大约阅读时间需要 7 分钟。

reference: 

Problem Definition:

Given an array in which all numbers except two are repeated once. (i.e. we have 2n+2 numbers and n numbers are occurring twice and remaining two have occurred once). Find those two numbers in the most efficient way.

Solution:

Let x and y be the non-repeating elements we are looking for and arr[] be the input array. First calculate the XOR of all the array elements.

xor = arr[0]^arr[1]^arr[2].....arr[n-1]

All the bits that are set in xor will be set in one non-repeating element (x or y) and not in other.So if we take any set bit of xor and divide the elements of the array in two sets – one set of elements with same bit set and other set with same bit not set. By doing so, we will get x in one set and y in another set. Now if we do XOR of all the elements in first set, we will get first non-repeating element, and by doing same in other set we will get the second non-repeating element.

Let us see an example.   arr[] = {2, 4, 7, 9, 2, 4}1) Get the XOR of all the elements.     xor = 2^4^7^9^2^4 = 14 (1110)2) Get a number which has only one set bit of the xor.      Since we can easily get the rightmost set bit, let us use it.     set_bit_no = xor & ~(xor-1) = (1110) & ~(1101) = 0010   Now set_bit_no will have only set as rightmost set bit of xor.3) Now divide the elements in two sets and do xor of            elements in each set, and we get the non-repeating    elements 7 and 9. Please see implementation for this      step.

Code:

/* This finction sets the values of *x and *y to nonr-epeating elements in an array arr[] of size n*/void get2NonRepeatingNos(int arr[], int n, int *x, int *y){  int xor = arr[0]; /* Will hold xor of all elements */  int set_bit_no;  /* Will have only single set bit of xor */  int i;  *x = 0;  *y = 0;   /* Get the xor of all elements */  for(i = 1; i < n; i++)   xor ^= arr[i];   /* Get the rightmost set bit in set_bit_no */  set_bit_no = xor & ~(xor-1);   /* Now divide elements in two sets by comparing rightmost set   bit of xor with bit at same position in each element. */  for(i = 0; i < n; i++)  {    if(arr[i] & set_bit_no)     *x = *x ^ arr[i]; /*XOR of first set */    else     *y = *y ^ arr[i]; /*XOR of second set*/  }}

转载地址:http://vexti.baihongyu.com/

你可能感兴趣的文章
万年历
查看>>
作为码农你希望面试官当场指出你错误么?有面试官这样遭到投诉!
查看>>
好多程序员都认为写ppt是很虚的技能,可事实真的是这样么?
查看>>
如果按照代码行数发薪水会怎样?码农:我能刷到公司破产!
查看>>
程序员失误造成服务停用3小时,只得到半月辞退补偿,发帖喊冤
查看>>
码农:很多人称我“技术”,感觉这是不尊重!纠正无果后果断辞职
查看>>
php程序员看过来,这老外是在吐糟你吗?看看你中了几点!
查看>>
为什么说程序员是“培训班出来的”就是鄙视呢?
查看>>
码农吐糟同事:写代码低调点不行么?空格回车键与你有仇吗?
查看>>
阿里p8程序员四年提交6000次代码的确有功,但一次错误让人唏嘘!
查看>>
一道技术问题引起的遐想,最后得出结论技术的本质是多么的朴实!
查看>>
985硕士:非科班自学编程感觉还不如培训班出来的,硕士白读了?
查看>>
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>