Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Find a Number occurring odd number of times in a Given array

Objec­tive: Given a array of inte­gers, in which every ele­ments occurs even num­ber of times except one num­ber which occurs add num­ber of times. Find out that number.

Exam­ple:

  int[] A = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7 };
Element appearing add number of times: 5

Approach:

we know that A XOR A = 0 so num­bers appear­ing even num­ber of times will be can­celled out and remain­ing ele­ment will the num­ber which is appear­ing odd num­ber of times.

Code:


Out­put:

Element appearing add number of times: 5

You may also like...

  • Varun Bhan­dari

    Code Does not work for this input : {2, 7, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2, 7, 4, 4};
    Returns ‘6’ which is not even in the array.

    • tuto­ri­al­hori­zon

      Because your input has two num­bers 3 and 5 which occurs odd num­ber of times. Objec­tive of prob­lem is ” Given a array of inte­gers, in which every ele­ments occurs even num­ber of times except one num­ber which occurs add num­ber of times”