Finding a number that repeats even no of times where all the other numbers repeat odd no of times

前端 未结 5 451
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 01:41

Given is an array of integers. Each number in the array repeats an ODD number of times, but only 1 number is repeated for an EVEN number of times. Find that number.

5条回答
  •  执笔经年
    2020-12-18 02:13

    You don't need to keep the number of times each element is found - just whether it's even or odd number of time - so you should be ok with 1 bit for each element. Start with 0 for each element, then flip the corresponding bit when you encounter the element. Next time you encounter it, flip the bit again. At the end, just check which bit is 1.

提交回复
热议问题