Wednesday, 19 July 2017

lonely integer (print the element who is single in the array )

Consider an array of integers, , where all but one of the integers occur in pairs. In other words, every element in occurs exactly twice except for one unique element.
Given , find and print the unique element.
Input Format
The first line contains a single integer, , denoting the number of integers in the array.
The second line contains space-separated integers describing the respective values in .
Constraints
  • It is guaranteed that is an odd number.
  • , where .
Output Format
Print the unique number that occurs only once in on a new line.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int lonelyinteger(int a_size, int* a) {
    int x=0;
    for (int arr_i=0;arr_i< a_size; arr_i++)
    {
        x=a[arr_i]^x;
    }
    return x;
    // Complete this function
}

int main() {
    int n;
    scanf("%i", &n);
    int *a = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%i",&a[a_i]);
    }
    int result = lonelyinteger(n, a);
    printf("%d\n", result);
    return 0;
}
 

No comments:

Post a Comment