logo CodeStepByStep logo

occurs_once

Write a function named occurs_once that accepts as a parameter an array of integers and its length, where each value in the array occurs exactly three times except for a single value that occurs only once. Your task is to return the integer value that occurs only once. For example, if the array a contains {3, 7, 3, 2, 3, 7, 7}, the call of occurs_once(a, 7) should return 2.

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. You may assume that an int contains exactly 32 bits.

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.