C Program to find Repeated Element in an array

[1512 views]



Often we have a requirement to find elements that are repeating in an array. So let's see a very simple program to find a repeated element in an array.

Code:

#include <stdio.h> #include <string.h> void Count_Repeated(int * arr, int size); int main() { int arr1[8] = {2,4,2,5,6,4,5,2}; Count_Repeated(arr1, 8); } void Count_Repeated(int * arr, int size) { int i, j, count; for (i = 0; i < size; i++) { count = 1; for (j = i + 1; j < size; j++) { if (arr[i] == arr[j] && arr[i] != 0) { count++; arr[j] = 0; } } if (count > 1 && arr[i] != 0) printf("%d repeated %d times \n", arr[i], count); } }

Output:

2 repeated 3 times
4 repeated 2 times
5 repeated 2 times

Online Tool to Replace Repeated Lines from Sentence
        



Facing Programming Errors. Get Answer within 48 hours:





Giving is the Master Key to Success, in all applications of human life.


Comments



Search
For Sponsored Advertisement OR Freelance Programming Article Submission, Contact us at atechdailyweb@gmail.com
Recommended Deals Ends in







Quiz Challenge:
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Find Duplicate Elements in Array using C

    Algorithm to find Duplicate Elements in an array