Here you compare each element one by one with every other element, why you get the answer as 4.
To count pairs for three or more three, one approach would be to sort the array, and then count the number of times this element occurred, and if the frequency is greater than 2, which means that it forms a pair. Here is the modified code:
#include<stdio.h>
main()
{
int arr[10], i, j, pairs = 0,count=0,prev_element=0;
int n;
scanf("%d", &n);
for(i = 0; i<n; i++)
{
scanf("%d", &arr[i]);
}
prev_element = arr[0];
count=1;
for(i = 1; i<n; i++)
{
if(prev_element==arr[i])
{
count++;
}
else
{
if(count>=2)
pairs+=count/2;
else
{
prev_element=arr[i];
count=1;
}
}
}
printf("%d", pairs);
}
. O (n2), O (n).
, -, , , , , .