logo CodeStepByStep logo

occursOdd

Language/Type: C++ bit algorithms
Related Links:

Write a function named occursOdd that accepts as a parameter a reference to a vector of integers, all of which occur an even number of times except for a single value that occurs an odd number of times. Your task is to find and return the integer value that occurs an odd number of times. For example, if the vector v contains {3, 7, 3, 8, 3, 7, 3}, the call of occursOdd(v) should return 8.

You must solve this problem using bitwise operators. Do not create any auxiliary collections such as vectors, arrays, lists, sets, etc. Also do not convert any integer into a string. An optimal solution can solve this problem with O(1) memory usage and in O(N) time where N is the number of elements in the vector.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.