user11822539
user11822539

Reputation:

Finding intersection of two arrays without allowing repeats

I am trying to find the intersection and union of two arrays without allowing repeats. I have the code for the intersection but my output is far from correct. I suspect the issue is with my nested for loops:

Here is my code:

#include <stdio.h>

int main() 
{   
  int i,j,n,k;

  printf("Enter size of the arrays: ");
  scanf("%d", &j);

  int first_array[j];
  int second_array[j];

  printf("Enter elements of first array: ");
  for(i=0; i<j; i++)
  {
      scanf("%d", &first_array[i]);
  }

  printf("Enter elements of second array: ");
  for(i=0; i<j; i++)
  {
      scanf("%d", &second_array[i]);
  }

  /*INTERSECTION & UNION*/
  int ii=-1,ui=-1;
  int intersect_array[2*n];
  int union_array[n];

  for(i=0; i<j; i++)
  {
      for(n=0; n<j; n++)
      {
          if(first_array[i] == second_array[n])
          {
              for(ii=0; ii<j; ii++)
              {
                  if(first_array[i] != intersect_array[ii])
                  {
                      intersect_array[ii] = first_array[i];
                  }
              }
          }
      }
  }

  printf("Intersection: ");
  for(i=0; i<=ii; i++)
      printf("%d",intersect_array[i]);
  printf("\n");

  return(0);
}

Here is my output for intersection when the input for array 1 is "1 2 3" and array 2 is "2 3 4":

Intersection: 3330

When my nested for loops were this, the output was correct except it allowed repeated numbers:

  for(i=0; i<j; i++)
  {
      for(n=0; n<j; n++)
      {
          if(first_array[i] == second_array[n])
          {
              intersect_array[ii] = first_array[i];
          }
      }
  }

Due to this, I know the issue is with the for loop and if statement that I added but I have been over them many times and I can't figure out why they aren't working. Is it because the added for loop is trying to initially iterate through an array with nothing in it?

Upvotes: 2

Views: 529

Answers (1)

idris
idris

Reputation: 588

Your repeat values check is wrong. Here is working version As soon as a repeat value found you should break the loop

#include <stdio.h>

int main() 
{   
int i,j = 3,n,k;

int first_array[3] = {1, 2, 3};
int second_array[3] = {2, 3, 4};



/*INTERSECTION & UNION*/
int ii=-1,ui=-1;
int intersect_array[3] = {0};
int union_array[3*2];
int intersectCount = 0;

for(i=0; i<j; i++)
{
    for(n=0; n<j; n++)
    {
        if(first_array[i] == second_array[n])
        {
            for(ii=0; ii<intersectCount; ii++)
            {
                if(first_array[i] == intersect_array[ii])
                    break;
            }
            // if it complete loop without break repeat value not found
            if (ii == intersectCount) 
               intersect_array[intersectCount++] = first_array[i];
        }
    }
}

printf("Intersection: ");
for(i=0; i<intersectCount; i++)
    printf("%d",intersect_array[i]);
printf("\n");

return(0);
  }

Upvotes: 1

Related Questions