I am trying to find a the union and intersection of arrays A and B. The union portion of my code is working, but the intersection is not working. Can anyone please help me figure out what is wrong with my intersection code?

Code:int main(void) { int A[5], B[5]; int C[5]; int lengthA = 0; int lengthB = 0; int i, j, k,sizeAuB,temp; // Code that takes in user input for set A for (i = 0; i < 5; ++i) { printf("Please input a number between [0, 10] into set A: "); scanf("%i", &temp); if (temp < 0 || temp > 10) { break; } else { A[i] = temp; lengthA = lengthA + 1; } } printf("The values for set A are:\n{"); for (i = 0; i < lengthA; ++i) { printf(" %i", A[i]); } printf(" }\n\n"); //Code to enter elements into set B for (i = 0; i < 5; ++i) { printf("Please input a number between [0, 10] into set B: "); scanf("%i", &temp); if (temp < 0 || temp > 10) { break; } else { B[i] = temp; lengthB = lengthB + 1; } } printf("The values for set B are:\n{"); for (i = 0; i < lengthB; ++i) { printf(" %i", B[i]); } printf(" }\n\n"); printf(" }\n\n"); printf("Set A is:\n{"); for (i = 0; i < lengthA; ++i) { printf(" %i", A[i]); } printf(" }\n\n"); printf("Set B is:\n{"); for (i = 0; i < lengthB; ++i) { printf(" %i", B[i]); } printf(" }\n\n"); // Finds the Union of A and B for (k = 0; k < lengthA; ++k) { C[k] = A[k]; } for (i = 0; i < lengthB; ++i) { for (j = 0; j < lengthA; ++j) { if (B[i] == C[j]) break; } if (j == lengthA) { C[k] = B[i]; k = k + 1; sizeAuB = k; } } // Sorts the elements that are Union of A and B from smallest to largest number. for (i = 0; i < sizeAuB - 1; ++i) { for (j = i + 1; j < sizeAuB; ++j) { if (C[j] < C[i]) { temp = C[j]; C[j] = C[i]; C[i] = temp; } } } printf("The union of A and B is:\n{ "); for (i = 0; i < sizeAuB; ++i) { printf(" %i", C[i]); } printf(" }\n\n"); printf(" The intersection is:\n{"); while (i<lengthA && j<lengthB) { if (A[i] < B[j]) i++; else if (B[j] < A[i]) j++; else { printf ("%i", B[j++]); i++; } } printf(" }\n\n"); return 0; }