Algorithms | Bit Algorithms | Question 1

Last Updated :
Discuss
Comments

What is the return value of following function for arr[] = {9, 12, 2, 11, 2, 2, 10, 9, 12, 10, 9, 11, 2} and n is size of this array. 

C++
int fun(int arr[], int n) {
    int x = arr[0];
    for (int i = 1; i < n; i++)
        x = x ^ arr[i];
    return x;
}
C
int fun(int arr[], int n)
{
    int x = arr[0];
    for (int i = 1; i < n; i++)
        x = x ^ arr[i];
    return x;
}
Java
public int fun(int[] arr) {
    int x = arr[0];
    for (int i = 1; i < arr.length; i++)
        x = x ^ arr[i];
    return x;
}
Python
def fun(arr):
    x = arr[0]
    for i in range(1, len(arr)):
        x ^= arr[i]
    return x
JavaScript
function fun(arr) {
    let x = arr[0];
    for (let i = 1; i < arr.length; i++)
        x ^= arr[i];
    return x;
}

0

9

12

2

Share your thoughts in the comments