Last active
April 12, 2019 21:03
-
-
Save donjajo/6d52c09717d250c338b8e425eed9bd8f to your computer and use it in GitHub Desktop.
Finds the non-repeated number in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
_Bool in_array(); | |
int *pop_array(); | |
void main() { | |
int numbers[] = { 1, 2, 2, 4, 4, 1, 5, 6, 5 }; | |
int length = sizeof( numbers ) / sizeof( numbers[ 0 ] ); | |
int lucky = 0; | |
for( int i = 0; i < length; i++ ) { | |
int *n_a = pop_array( i, numbers, length ); | |
if( !in_array( numbers[ i ], n_a, (length-1) ) ) { | |
lucky = numbers[ i ]; | |
} | |
free( n_a ); | |
} | |
printf( "Lucky number is %d", lucky ); | |
} | |
_Bool in_array( int value, int *a, int length ) { | |
for( int i = 0; i < length; i++ ) { | |
if( value == *( a+i ) ) | |
return 1; | |
} | |
return 0; | |
} | |
int *pop_array( int index, int a[], int length ) { | |
int *new_array = malloc( sizeof( int ) * (length ) ); | |
int i = 0; | |
int p_i = 0; | |
while( i < length ) { | |
if( i == index ) { | |
*(new_array+i) = a[i+1]; | |
i++; | |
continue; | |
} | |
*(new_array+p_i) = a[i]; | |
i++; | |
p_i++; | |
} | |
return new_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment