count duplicate elements in array code example
Example 1: hopw to check how many duplicates in an array c
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, j, size, count = 0;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
count++;
break;
}
}
}
printf("\nTotal number of duplicate elements found in array = %d", count);
return 0;
}
Example 2: duplicate string in array
let strArray = [ "q", "w", "w", "w", "e", "i", "u", "r"];
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
console.log(findDuplicates(strArray))
console.log([...new Set(findDuplicates(strArray))])