Monday 25 September 2017

Given an array filled with integers that appear exactly twice, with the exception of one integer that appears once, find the unique integer. ex:findUnique([1, 2, 6, 9, 9, 1, 3, 6, 2]) returns: 3 ex: findUnique([12, 45, 32, 65, 32, 65, 45]) returns: 12

#include<stdio.h>
#include<stdlib.h>


int main(){
    int i,*arr,len,temp,j;
    scanf("%d",&len);
   
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
   
    for(i=0;i<len;i++){
        scanf("%d",&arr[i]);
    }
   
    //Sort the array
    for (i=0;i<len;i++)
    {
        for (j=0;j<len-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
     
    //Traversing the array, finding the unique integer as mentioned in the question the number must
    //appear exactly twice except one unique number
    for(i=0;i<len-1;i+=2){
        if(arr[i]!=arr[i+1]){
            printf("%d is the unique number\n",arr[i]);
            break;
        }
    }
}

1 comment:

  1. a1 positive

    Hopefully many of you have come across the people with different eye colour. Dark brown is the most common one, while we also get to see blue, green/hazel and light brown too.

    to get more - https://www.scienceofbiogenetics.com/tag/twodifferentcolored-eyes-spiritual-meaning/

    ReplyDelete