Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- int main(void) {
- int N, M;
- printf("Insert N, M: ");
- scanf("%d %d", &N, &M);
- // VLA V1, V2
- int V1[N];
- int V2[M];
- // Loading V1
- for (int i = 0; i < N; i++) {
- scanf("%d", &V1[i]);
- }
- // Loading V2
- for (int i = 0; i < M; i++) {
- scanf("%d", &V2[i]);
- }
- // Declaring common array
- int max_len = N * (N > M) + M * (M >= N); // max (M, N)
- // Common items array
- int common[max_len];
- int common_items = 0;
- // For each item in V1
- for (int i = 0; i < N; i++) {
- // If item exists in V2 as well and not in common array
- bool is_in_v2 = false;
- // Current item exists in v2
- for (int j = 0; j < M && !is_in_v2; j++) {
- if (V1[i] == V2[j]) {
- is_in_v2 = true;
- }
- }
- // If in v2 check for existance in common array
- bool is_in_common = false;
- for (int j = 0; j < common_items && !is_in_common; j++) {
- if (V1[i] == common[j]) {
- is_in_common = true;
- }
- }
- if (is_in_v2 && !is_in_common) {
- common[common_items] = V1[i];
- common_items++;
- }
- }
- printf("Duplicates: \n[ ");
- for (int i = 0; i < common_items; i++) {
- printf("%d ", common[i]);
- }
- printf("]");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement