In this tutorial you will learn about the C++ Programs to Find Duplicate Array Element and its application with practical example.
In this tutorial, we will learn to create a c++ program that find the Duplicate Array Element using c++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators.
- looping statements.
- Basic input/output.
- Basic c++ programming
- For Loop.
- Array.
- Function.
What Is Array ?
An Array is a collection of similar data type containing collection of elements (values or variables), each identified by one Array index value, Array is a data structure that hold collection of elements.
C++ Programs to Find Duplicate Array Element.
In this program ,we will find duplicate elements in a Array .First of all user will be prompted to enter a the size or Array and after that user will give elements and we will find the repeating elements in a given Array. Let see the code for finding duplicate values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> using namespace std; void duplicate_element(int arr[], int num) // finding duplicate values { int i, j; printf("Repeating elements are following: "); for(i = 0;i < num;i++) { for(j = i+1;j < num;j++) { if(arr[i] == arr[j]) { cout << arr[j] << " "; } } } } int main() { int no; cout<<"enter the size of array"; cin >> no; // taking size of array int ele[no]; int i; cout<<"\n enter elements:"; // taking elements. for(i = 0; i < no; i++) { cin >> ele[i]; } duplicate_element(ele,no); // passing value to function return 0; } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- no = it will hold size of array
- ele = it will hold a value of array.
- i & j = for iteration.
- arr= it will hold a value of array.
- num= it will hold total number of array value.
And in the next statement user will be prompted to enter size of a Array and value of Array.which will be assigned to variable ‘no’ and in ‘ele’ respectively.
and
Now ,loop through the size given by user and take all values supply by user in ele variable.Afterword pass this value to function.
and within the function in each iteration we will compare values of array to find the occurrence of a similar elements and each matching condition we print the matched values .
This process will continue until loop ends within the function, and at last we will print the total number occurrence of a Duplicate values.