0

I need to know a method to keep duplicate numbers from being stored in a new array when taking numbers from two different arrays. The function is supposed to store each 'unique' value once and not store duplicate values again.

Here is my function code so far:

int * arrayIntersect(int *sizeOfResult, const int *a, const int *b, int sizeOfA, int sizeOfB){
int i;
int j;
int k = 0;
int c[(sizeOfA + sizeOfB)];

    for(j = 0; j < sizeOfB; j++){
        for(i = 0; i < sizeOfA; i++){
            if(a[i] == b[j]){
                c[k] = a[i];
                (*sizeOfResult)++;
                k++;
            }   
        }
    }
int *d = (int *)malloc(sizeof(int) * *sizeOfResult);
    for(i = 0; i < *sizeOfResult; i++){
        d[i] = c[i];
    }
return d;

}

It prints the values I need, but I want to eliminate the same number from showing up multiple times when printing the contents of the new dynamic array.

Any idea on how to improve my code to allow prevent duplication?

  • In the sample you are actually storing duplicates only. Isn't that what you want to prevent? – imreal Oct 26 '12 at 00:49
  • I worded the question incorrectly. I am supposed to select each 'unique' value and store it but the program is supposed to ignore duplicates of those values. – user1775823 Oct 26 '12 at 00:57
  • Exactly, but you are storing when they are duplicates if(a[i] == b[j]) – imreal Oct 26 '12 at 01:01
  • Changing it to if(a[i] != b[j]) results in many duplications, how would I get rid of same values? – user1775823 Oct 26 '12 at 01:04
  • If the arrays are ordered, you can do that in linear time. Otherwise, you can do in `O(max(sizeOfA, sizeOfB)lgmax(sizeOfA, sizeOfB))`. – Murilo Vasconcelos Oct 26 '12 at 01:06
  • Changing it to that won't solve it either, all I am saying is the way it is now, you're just storing a number when two numbers are equal. – imreal Oct 26 '12 at 01:06
  • What will fix my problem then, I have been staring at this for quite some time and feel like the answer is an easy one, just not sure what to fix. Murilo what do you mean by that snippet? – user1775823 Oct 26 '12 at 01:11

2 Answers2

0

The proper way to do it is having the arrays ordered and then doing a binary search for each insertion like @Murilo Vasoncelos pointed out.

Below is a quick and dirty solution that loops through a and b and for each iteration checks if the number has been inserted before. If it isn't, it inserts it.

int duplicate = 0;
*sizeOfResult = 0;
for(j = 0; j < sizeOfA; j++){
    for(i = 0; i < (*sizeOfResult); i++){
        if(c[i] == a[j]){
            duplicate = 1;
            break;
        }   
    }
    if (!duplicate)
    {
        c[(*sizeOfResult)] = a[i];
        (*sizeOfResult)++;
    }
    duplicate = 0;
}
for(j = 0; j < sizeOfB; j++){
    for(i = 0; i < (*sizeOfResult); i++){
        if(c[i] == b[j]){
            duplicate = 1;
            break;
        }   
    }
    if (!duplicate)
    {
        c[(*sizeOfResult)] = b[i];
        (*sizeOfResult)++;
    }
    duplicate = 0;
}
imreal
  • 10,178
  • 2
  • 32
  • 48
0

If your arrays a and b are ordered, you can simply use this linear algorithm for array intersection:

int* inter(int* szr, int* a, int* b, int sza, int szb)
{
    int c[MAX(sza, szb)];
    int i, j, k = 0;

    for (i = 0, j = 0; i < sza && j < szb;) {
        if (a[i] == b[j]) {
            if (k == 0 || c[k - 1] < a[i]) {
                c[k++] = a[i];
            }

            i++;
            j++;
        }
        else if (a[i] < b[j]) {
            i++;
        }
        else {
            j++;
        }
    }

    *szr = k;

    int* ans = (int*)malloc(sizeof(int) * k);
    for (i = 0; i < k; ++i) {
        ans[i] = c[i];
    }

    return ans;
}
Murilo Vasconcelos
  • 4,677
  • 24
  • 27