Count duplicate elements in an array

count duplicate elements in an array ..... input {1,1,1,1,2,2,2,3,3,4} output

 1=4 2=3 3=2 4=1 
+1
c arrays count
source share
8 answers
 prev = input[0]; count = 1; for (i = 1; i < ARRAYSIZE; i++) { if (input[i] == prev) count++; else { printf("%d=%d ", prev, count); prev = input[i]; count = 1; } } // Printing the last element printf("%d=%d ", prev, count); 
+3
source share

if it is already sorted, there may be a faster way

Divide the list in half. look at each section this way: check the first and last nubmer. If they are the same, you know the result. if it is not the same, divide it in the middle and repeat each half again.

With long runs of the same number, this will be effective. It should return to the method one by one if the section is small. You can sample data at random points by testing s, s + 1 to get a percentage of the time when the number increases from its predecessor. The higher the number, the sooner you should switch to the "one by one" method.

This method can also be parallelized.

+2
source share

It seems the array is sorted (according to the example). If so, you just need to select the first element and iterate through the array until a specific value is found. Inside this process, you can have a counter in the loop to count events.

Then select the excellent value found instead of the first item and repeat the process.

+1
source share

If the range of values ​​is small, you can have another array containing the count of each element, much like the sort count . If the range of values ​​is large, you will need a hash table .

+1
source share

A good method is to iterate over the elements in the array and count the amount of each that you see. Once you are done, you will print out the invoice that you have.

0
source share
 void count_elements(int * pArray, long nElements) { int * pStart; for ( pStart = pArray++; nElements > 1; nElements--, pArray++ ) { if ( *pStart != *pArray ) { printf("%i = %u ", *pStart, (pArray - pStart)); pStart = pArray; } } printf("%i = %u ", *pStart, (pArray - pStart)); } 
0
source share

A simple example.

// Count each element, create a sorted hash to store each unique element from the source array (can also be made as a linked list); read each element from the array if the hash element already exists; increase the number (value) of this key if the hash element does not exist create a key value pair (value = 1) loop

// Print each element loop through the hash key and printf ("% d:% d \ n", key, value); if you also need to represent null values, implement lastKey and make a key to compare lastKey to determine if the key was null

Sort and simple program / function

0
source share

processed:
1) end of array element
2) empty case
3) singleton case

 #include <stdio.h> int main() { int input[] = {1,1,1,1,2,2,2,3,3,4}; if (sizeof(input) == 0) return 0; int prev = input[0]; int count = 1; int i; int ARRAYSIZE = sizeof(input) / sizeof(int); for (i = 1; i < ARRAYSIZE; i++) { if (input[i] == prev) { count++; } else { printf("%d=%d ", prev, count); prev = input[i]; count = 1; } } printf("%d=%d\n", prev, count); return 0; } 

and test cases:

 when input is {} ----------------------------------- jianlin@ubuntu :~$ gcc try.c jianlin@ubuntu :~$ ./a.out when input is {123} ----------------------------------- jianlin@ubuntu :~$ gcc try.c jianlin@ubuntu :~$ ./a.out 123=1 when input is {1,123} ----------------------------------- jianlin@ubuntu :~$ gcc try.c jianlin@ubuntu :~$ ./a.out 1=1 123=1 when input is {1,1,1,1,2,2,2,3,3,4} ----------------------------------- jianlin@ubuntu :~$ gcc try.c jianlin@ubuntu :~$ ./a.out 1=4 2=3 3=2 4=1 when input is {1,1,1,1,2,2,2,3,3,4,4} ----------------------------------- jianlin@ubuntu :~$ gcc try.c jianlin@ubuntu :~$ ./a.out 1=4 2=3 3=2 4=2 
0
source share

All Articles