Write a program to print all repeated elements in an array and also print the count of repeated numbers
Note: Hope it helps you! Please note that all the intentions are to clear the doubt of how to solve it in java. So, please try to give it a chance at least before using this. If you face any discrepancy please contact us. Thank you!
Example:
input:
Enter the number of element: 4
Enter Elements: 10
20
10
20
output:
The duplicate number is: 10
The duplicate number is: 20The total number of the duplicate element is: 2
Algorithm of this program:
- Initially, we define the size of the array.
- Then, take a user-input size of the array and scan the element of the array.
- For finding the duplicate number in the array we have to run two for loops.
- The first for loop scan all elements from 0 index
- The next for loops run from i+1 so that it checks all the duplicate elements.
- If any duplicate element is found, we print the duplicate number and count it.
- At last, we print the count of all the duplicate numbers present in the array.
- The return type of the main function is int so, in the last, we return 0.
Program of the problem:
//Write a program to print all repeated elements in an array and also print the count of //repeated numbers#include <stdio.h>#define SIZE 100int main(){int arr[SIZE];int size;int count = 0;printf("Enter the number of elements in array: ");scanf("%d", &size);printf("Enter the value of elements in array: ");for(int i=0; i<size; i++){scanf("%d", &arr[i]);}for(int i=0; i<size; i++){for(int j=i+1; j<size; j++){if(arr[i] == arr[j]){printf("The duplicate numbers is: %d\n", arr[i]);count++;break;}}}printf("\nTotal number of duplicate elements found in array = %d", count);return 0;}//code is contributed by paagalblogger.blogspot.com
Gd job
ReplyDeleteThank you @tarif
DeleteThank you so much❤
ReplyDeleteThis really helped me a lot❤
Thank you Rahul
ReplyDelete