Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to perform intersection operation on two arrays
In C programming, an array intersection operation finds all common elements between two arrays. The intersection of two arrays contains only the elements that appear in both arrays, without duplicates.
Syntax
for(i=0; i<size1; i++) {
for(j=0; j<size2; j++) {
if(array1[i] == array2[j]) {
intersection[k] = array1[i];
k++;
}
}
}
How It Works
If Array1 = {1, 2, 3, 4, 6} and Array2 = {1, 2, 5, 6, 7}, then:
Array1 ? Array2 = {1, 2, 3, 4, 6} ? {1, 2, 5, 6, 7}
= {1, 2, 6}
The algorithm compares each element of the first array with every element of the second array. When a match is found, the element is added to the intersection array.
Example: Basic Array Intersection
Here's a complete program that finds the intersection of two arrays −
#include <stdio.h>
void sortArray(int size, int arr[]) {
int i, j, temp;
for(i = 0; i < size; i++) {
for(j = i + 1; j < size; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int removeDuplicates(int size, int arr[]) {
int i, j, k;
for(i = 0; i < size; i++) {
for(j = i + 1; j < size;) {
if(arr[i] == arr[j]) {
for(k = j; k < size - 1; k++) {
arr[k] = arr[k + 1];
}
size--;
} else {
j++;
}
}
}
return size;
}
int main() {
int arr1[] = {4, 5, 6, 7, 8};
int arr2[] = {4, 1, 6, 9};
int size1 = 5, size2 = 4;
int intersectionSize = (size1 < size2) ? size1 : size2;
int intersection[intersectionSize];
int k = 0, i, j;
printf("Array 1: ");
for(i = 0; i < size1; i++) {
printf("%d ", arr1[i]);
}
printf("<br>");
printf("Array 2: ");
for(i = 0; i < size2; i++) {
printf("%d ", arr2[i]);
}
printf("<br>");
// Find intersection
for(i = 0; i < size1; i++) {
for(j = 0; j < size2; j++) {
if(arr1[i] == arr2[j]) {
intersection[k] = arr1[i];
k++;
break; // Avoid adding same element multiple times
}
}
}
// Sort and remove duplicates
sortArray(k, intersection);
int finalSize = removeDuplicates(k, intersection);
printf("Intersection: ");
if(finalSize > 0) {
for(i = 0; i < finalSize; i++) {
printf("%d ", intersection[i]);
}
} else {
printf("No common elements");
}
printf("<br>");
return 0;
}
Array 1: 4 5 6 7 8 Array 2: 4 1 6 9 Intersection: 4 6
Method 2: Using Hash Array
For better efficiency with small positive integers, we can use a hash array approach −
#include <stdio.h>
#define MAX_VALUE 100
int main() {
int arr1[] = {1, 2, 3, 4, 6};
int arr2[] = {1, 2, 5, 6, 7};
int size1 = 5, size2 = 5;
int hash[MAX_VALUE] = {0};
int i;
printf("Array 1: ");
for(i = 0; i < size1; i++) {
printf("%d ", arr1[i]);
hash[arr1[i]] = 1; // Mark elements of first array
}
printf("<br>");
printf("Array 2: ");
for(i = 0; i < size2; i++) {
printf("%d ", arr2[i]);
}
printf("<br>");
printf("Intersection: ");
for(i = 0; i < size2; i++) {
if(hash[arr2[i]] == 1) {
printf("%d ", arr2[i]);
hash[arr2[i]] = 0; // Avoid printing duplicates
}
}
printf("<br>");
return 0;
}
Array 1: 1 2 3 4 6 Array 2: 1 2 5 6 7 Intersection: 1 2 6
Time Complexity
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Nested Loop | O(n × m) | O(min(n,m)) |
| Hash Array | O(n + m) | O(max_value) |
Conclusion
Array intersection in C can be implemented using nested loops for general cases or hash arrays for better performance with small integers. The nested loop approach works with any data type but has higher time complexity.
