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
Array elements that appear more than once?
In C programming, finding array elements that appear more than once is a common problem. Given an array, we need to identify which elements have a frequency greater than 1. For example, in the array {1, 5, 2, 5, 3, 1, 5, 2, 7}, elements 1, 2, and 5 appear multiple times, so they should be included in our result.
Syntax
void findDuplicates(int arr[], int n);
Algorithm
Begin
Create frequency array to count occurrences
For each element in array:
Increment its frequency count
For each element:
If frequency > 1, print the element
End
Method 1: Using Frequency Array
This approach uses an additional array to count the frequency of each element −
#include <stdio.h>
#include <stdbool.h>
void findDuplicates(int arr[], int n) {
int max_val = arr[0];
/* Find maximum value in array */
for (int i = 1; i < n; i++) {
if (arr[i] > max_val) {
max_val = arr[i];
}
}
/* Create frequency array */
int freq[max_val + 1];
for (int i = 0; i <= max_val; i++) {
freq[i] = 0;
}
/* Count frequencies */
for (int i = 0; i < n; i++) {
freq[arr[i]]++;
}
/* Print elements with frequency > 1 */
bool first = true;
for (int i = 0; i <= max_val; i++) {
if (freq[i] > 1) {
if (!first) printf(" ");
printf("%d", i);
first = false;
}
}
printf("<br>");
}
int main() {
int arr[] = {1, 5, 2, 5, 3, 1, 5, 2, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Elements with frequency more than one: ");
findDuplicates(arr, n);
return 0;
}
Elements with frequency more than one: 1 2 5
Method 2: Using Nested Loops
This approach compares each element with every other element to count occurrences −
#include <stdio.h>
#include <stdbool.h>
void findDuplicatesNested(int arr[], int n) {
bool printed[n];
for (int i = 0; i < n; i++) {
printed[i] = false;
}
printf("Elements with frequency more than one: ");
bool first = true;
for (int i = 0; i < n; i++) {
if (printed[i]) continue;
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
printed[j] = true;
}
}
if (count > 1) {
if (!first) printf(" ");
printf("%d", arr[i]);
first = false;
}
printed[i] = true;
}
printf("<br>");
}
int main() {
int arr[] = {1, 5, 2, 5, 3, 1, 5, 2, 7};
int n = sizeof(arr) / sizeof(arr[0]);
findDuplicatesNested(arr, n);
return 0;
}
Elements with frequency more than one: 1 5 2
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Frequency Array | O(n + max_value) | O(max_value) | Small range of values |
| Nested Loops | O(n²) | O(n) | Large range of values |
Key Points
- The frequency array method is more efficient for arrays with small value ranges.
- The nested loop approach works for any value range but has higher time complexity.
- Both methods preserve the order of first occurrence of duplicate elements.
Conclusion
Finding duplicate elements in an array can be solved using different approaches. The frequency array method offers better time complexity for small ranges, while nested loops provide a space-efficient solution for larger ranges.
