piby180
piby180

Reputation: 388

Find the lonely integer in an array

Please refer to this hackerrank challenge if you can.

The problem is to find the lonely integer in an array, given an array consists of only pairs except one lonely integer.

The problem is with this test case

9
4 9 95 93 57 4 57 93 9

9 is array size and below is the array

See the part of code highlighted by //------

If I place scanf("%d", &n) above int arr[n], the code works fine, but it gives horrible results the other way round.

#include <stdio.h>

int lonely_integer(int* a, int size);

int main(){
    //n is size of array, i is counter variable
    int n, i, result;
    // ---------------------
    int arr[n];
    scanf("%d", &n);
    // ---------------------
    printf("%d\n", n);
    for(i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }
    result = lonely_integer(arr, n);
    printf("%d", result);
    return 0;
}


int lonely_integer(int* a, int size){
    int i;
    int res = 0;
    for(i = 0; i < size; i++){
        res = res ^ a[i];
    }

    return res;
}

Upvotes: 4

Views: 9458

Answers (6)

D&#250;thomhas
D&#250;thomhas

Reputation: 10083

For a maximum of 100 elements, there is no need to dynamically allocate an array (either through malloc or using a VLA). Just create an array of 100 elements.

Then you only need to use N elements.

  int arr[100];  // input will be 1..100 elements

  int size;
  scanf( "%d", &size );  // input is in 1..99

  for (int i = 0;  i < size;  i++)
    scanf( "%d", &arr[i] );

Looking at your solution, though, why bother with an array? Just compute the lonely integer.

#include <stdio.h>

int get_int(void)
{
  int d;
  scanf( "%d", &n );
  return d;
}

int main(void)
{
  int n = get_int();

  int lonely = 0;
  while (n --> 0)
    lonely ^= get_int();

  printf( "%d\n", lonely );
}

Upvotes: 0

mertagcakoyun
mertagcakoyun

Reputation: 11

You can use Kotlin. Group all items and get their number with eachCount method. It will return a Map<element, Int>. If we know there will be only 1 unique element. we can take first or last element of keys of map.

 
fun lonelyinteger(a: Array<Int>): Int {
        return a.groupingBy { it }.eachCount().filter { it.value==1 }.keys.first()
    }

Upvotes: 0

Giriraj Pawar
Giriraj Pawar

Reputation: 461

Value of 'n' has to be defined before it has used.like you are using

int arr[n];

before reading the value of 'n'. So compiler will not know, how many number of elements are present in the array 'n' can be a garbage value.how much amount of memory it should allocate to an array.

hence you gotta read the value of 'n' before using it an array definition.

Upvotes: 0

haccks
haccks

Reputation: 106102

Range of n is given in the question is 1 <= N < 100 which is small and a variable length array can be used. But you are doing wrong here

int arr[n];   // n is uninitialized. Its value is indeterminate.
scanf("%d", &n);  

You need to initialize n before using it as array size

scanf("%d", &n);
int arr[n];

Upvotes: 2

user3079266
user3079266

Reputation:

You'd want to use:

#include <stdlib.h>
/* ... */
int *arr;
scanf("%d", &n);
arr = malloc(sizeof(int) * n);

This way, arr gets dynamically allocated at runtime, so it can be of any size depending on the input n.

What you were originally doing (i.e. declaring arr[n] after recieving n via scanf: scanf("%d", &n); int arr[n];) is not a good idea because it makes use of Variable-Length Arrays, a feature of C that is not mandatory in the latest C standard.

You see, arr gets created at compile-time, and you normally can only initialize it with a constant expression known at compile-time, which n, a variable recieved as user input, obviously isn't. Variable-length arrays are a feature of the language that basically allows you to bypass this rule, i.e. they make you able to initialize an array to a length not known at compile-time. This has been standartized in C99, but was listed as "optional" as of C11.

What you did after that (int arr[n]; scanf("%d", &n);) is quite illogical because, well, you declare arr as an array of n integers before you recieve the value of n as user input, and, well, know its value. It prints garbage because n is originally initialized to an unspecified "garbage" value, and this is what your VLA's size becomes when you declare it:

int arr[n]; //n is garbage at this point, you have no idea how large arr will be!
scanf("%d", &n); //you got the value of n that you needed, but too late, alas!

Upvotes: 1

Sridhar Nagarajan
Sridhar Nagarajan

Reputation: 1105

Allocating array with a uninitialized variable, will lead to undefined behavior and the compiler will throw a warning "variable used uninitialized in this function"

If you are getting the size of array at runtime, it would be wise to use dynamic memory allocation as @Mints97 posted

int data_size;
int *data_array;
scanf("%d", &data_size);
data_array = (int*)calloc(data_size,sizeof(int));
/*
 .
*/
// Free the memory at the end
free(data_array);
data_array = NULL;

If you want to set size of array at compile time, you can define a macro

#define DATA_SIZE 9

or set the macro while compiling the code

gcc test.c -o test -DDATA_SIZE=9 -Wall

Upvotes: 0

Related Questions