# Thread: check for uniqueness in arrya

1. ## Check for uniqueness in array

I Have 2 one dimensional arrays. One array is catalogue no.
(A) the other is quantity (D). I need to sum up all elements from the same catalogue no.
This is what I wrote:
Code:
```#include<stdio.h>

#define N 6

#define K 3

int f(int A[N]);

int  i,k;

void main()

{
int A[N]={4444, 1111, 2222, 1111 ,3333, 4444};

int D[N]= {1, 3, 7, 2, 8, 3};
int sum[N]={0};

for (i=0;i<N;i++)
{
k=f(A)+1;
if (k==0)
sum[i]=D[i];
else
sum[k]+=D[k];
}

printf("\nA sub array found in row i = %d, \n", f(A)+1);

for (i=0;i<N;i++)
printf("\nsum is = %d, \n", sum[i]);

}

int f(int A[N])
{
int i, j, c=0;

for (i = 0; i < N ; i++)
for (j = 1 ; j < N ; j++)

if (A[i] == A[j] )
c = i;

return (c);

}```
It is clear to me my uniqueness algorithm is false. Looked up google & this forum & couldn’t come to conclusion how should I write it.

2. I don't understand what you're trying to do. What do you mean "uniqueness algorithm"? Are you trying to, given a value, see if it exists more than once in an array?
Code:
```for x = 0; x < number of elements in the array; x++
if array[x] is the number to find
found = true
foundhere = x;
else
figure out if you want to stop looking or not because it's already been found```
Is something like that what you had in mind?

Quzah.

3. did not exactly understand your code...
what I need is to check in the array of catalugue numbers (A) if this element exist. If it is not then intialize the value of sum array with the number in corresponding place from array D. If it is exists ad it to the the place where the poriginal was started.
Hope I’m clearer now…

4. If I did understand correctly, what you want is something along this:

Code:
```#include <stdio.h>

#define ARRAY_SIZE(array)       (sizeof(array) / sizeof(*array))

int main(void)
{
struct {
unsigned int number;
unsigned int quantity;
} catalogue[] = {
{ 4444, 1 },
{ 1111, 3 },
{ 2222, 7 },
{ 1111, 2 },
{ 3333, 8 },
{ 4444, 3 }
};
unsigned int i, j;

for (i = 0; i < ARRAY_SIZE(catalogue); i++)
for (j = 0; j < ARRAY_SIZE(catalogue); j++)
if (i != j && catalogue[i].number == catalogue[j].number) {
catalogue[i].quantity += catalogue[j].quantity;
catalogue[j].quantity = 0;
}

for (i = 0; i < ARRAY_SIZE(catalogue); i++)
printf("%d = %d\n", catalogue[i].number, catalogue[i].quantity);

return 0;
}```