Tuesday, 23 September 2014

Hackerrank Lonely Integer

The Solution for hackerrank problem, Lonely Integer using C Program.
Passed Test cases: 8 out of 8

SOURCE CODE:

 #include <stdio.h>  
 #include <string.h>  
 #include <math.h>  
 #include <stdlib.h>  
 #include <assert.h>  
 int lonelyinteger(int a_size, int* a) {  
   int flag=1,i,k;  
   for(i=0;i<a_size;i++)  
   {  
     flag=1;  
     for(k=0;k<a_size;k++)  
     {  
       if(i != k && a[i] == a[k])  
        {  
            flag=0;        
        }  
     }  
     if(flag)  
     {  
        return a[i];     
     }  
   }  
   return 0;  
 }  
 int main() {  
   int res;  
   int _a_size, _a_i;  
   scanf("%d", &_a_size);  
   int _a[_a_size];  
   for(_a_i = 0; _a_i < _a_size; _a_i++) {   
     int _a_item;  
     scanf("%d", &_a_item);  
     _a[_a_i] = _a_item;  
   }  
   res = lonelyinteger(_a_size, _a);  
   printf("%d", res);  
   return 0;  
 }  

2 comments:

  1. #!/usr/bin/py
    def lonelyinteger(arr):
    final = 0
    for x in xrange(0,len(arr)):
    final ^=arr[x]
    return final


    print lonelyinteger([10,2,2,40,10,9,9])

    ReplyDelete
  2. #include
    using namespace std;
    int main(){

    int a[100];
    int result = 0,n;
    cin>>n;
    if(n%2==1)
    {

    for (int i=0;i>a[i];
    result ^= a[i];
    }
    cout<<result;
    }



    }

    ReplyDelete