Program to find repeating element in an array (duplicate elements)
Identifying duplicate or repeating elements in an array is a common problem that programmers often face. This article will guide you through an easy approach to detect repeating elements in an array using nested loops. By the end of this, you will be able to efficiently find and handle duplicate values in an array.
Algorithm for Identifying Duplicate Elements
The approach we will use involves two loops. The first loop is responsible for traversing through each element of the array, while the second loop checks if the current element has already been encountered.
Steps:
Input: Take an array of size n.
Loop Through the Array: Traverse through the array and for each element:
Check if this element has been encountered before (using a second loop).
If it has been encountered, print it as a duplicate and continue.
If not, proceed to the next element.
Output: The repeating elements are printed.
Code Example in C
Below is the C program that demonstrates how to find repeating elements in an array using nested loops:
c
#include<stdio.h>voidfindDuplicates(int arr[], int n) {
printf(“Repeating elements are: \n”);
// Traverse the arrayfor (int i = 0; i < n; i++) {
// Check if the current element is repeatedfor (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
printf(“%d “, arr[i]);
break;
}
}
}
}intmain() {
int arr[] = {4, 3, 2, 7, 8, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);findDuplicates(arr, n);return0;
}
Output:
sql
Repeating elements are:
43
Code Example in C++
Here’s the C++ program for identifying repeating elements in an array:
cpp
#include<iostream>usingnamespace std;voidfindDuplicates(int arr[], int n){
cout << “Repeating elements are: \n”;
// Traverse the arrayfor (int i = 0; i < n; i++) {
// Check if the current element is repeatedfor (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
cout << arr[i] << ” “;
break;
}
}
}
}intmain(){
int arr[] = {4, 3, 2, 7, 8, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);findDuplicates(arr, n);return0;
}
Output:
sql
Repeating elements are:
43
Recommended Programs Related to Arrays
If you’re interested in working with arrays, here are a few related programs you may find useful:
Count Distinct Elements of an Array:
A program that counts the number of unique (non-repeating) elements in an array.
Find Non-Repeating Elements of an Array:
Identifies elements that appear only once in an array.
Remove Duplicate Elements in an Array:
A program to remove duplicates and return an array with only unique elements.
Find Repeating Elements in an Array:
A more advanced version of this program that handles larger datasets.
Key Points to Consider
Time Complexity: The above approach uses two nested loops, which gives it a time complexity of O(n^2). This could be inefficient for large arrays.
Space Complexity: The space complexity is O(1) because no extra space is used except for the input array.
Conclusion
This simple two-loop approach allows you to identify repeating elements in an array quickly. While it may not be the most efficient solution for larger arrays, it provides a straightforward and easy-to-understand method for detecting duplicates. For larger datasets, you might want to explore more advanced algorithms or use data structures like hash maps to improve efficiency.Click here to know more our program!