Need help finding a unique value in array

Posted by bardockyo on Stack Overflow See other posts from Stack Overflow or by bardockyo
Published on 2012-09-20T03:11:54Z Indexed on 2012/09/20 3:37 UTC
Read the original article Hit count: 93

Filed under:
|
|

My code is complete minus one little flaw. It searches the array and prints out which values are unique, however it always counts the first entry as unique even if it is followed by the same value. Can anyone look at my code and tell me which part is messing this up because it is driving me crazy.

    #include <stdio.h>
    #define size 7
    int main(void)
    {
        int array1[size], target, answer, found, x, k, prev, count =1, i;

        printf("Please input %d integers: ", size);
        scanf("%d", &target);

        for(x = 0; x < size; x++)
        {
             scanf("%d", &array1[x]);

        }

        prev = array1[0];

        for (i = 1; i < size; i++) 
        {
             if (array1[i] == prev) 
             {
                 count++;
             } 
             else 
             {
                 if (count < 2)
                     printf("%d=%d\n", prev, count);
                 prev = array1[i];
                 count = 1;
             }

        }

        if (count < 2)
        {
             printf("%d=%d\n", prev, count);
        }

        return 0;
   }

© Stack Overflow or respective owner

Related posts about c

    Related posts about arrays