Products > Programming

counting duplicates numbers in array

(1/7) > >>

Kittu20:
I am trying to write code that can count duplicate numbers in array for following sequences

1 2 1 2 5   2 found 1 times
1 2 2 2 5   2 found 2 times
1 2 3 4 5    not found
9 9 9 9 9   9 found 4 times

I am trying for first sequence


--- Code: ---#include<stdio.h>

int main()
{
    int array[5] = {1, 2, 1, 2, 5 };

for ( int i = 0; i < 5; i++)
{
for ( int j = i + 1; j < 5; j++)
{
if ( array[i] == array[j])
{

}

}
}

return 0;
}
--- End code ---

How to count  duplicates numbers in array

DavidAlfa:
How about this.
You don't need to compare anything, just make another array with counts.
At the end of the loop you'll have the counts of each number.
You can check if the counter is <2, omitting the output as there were no duplicates.

--- Code: ---uint8_t array[];
uint8_t count[10] = {0};
for (uint8_t i=0; i<sizeof(array); i++){
  count[ array[i] ]++;
}
for (uint8_t i=0; i<10; i++){
  printf("%u found %u times\n", i, count[i]);
}

--- End code ---

Change uint8_t  to uint16_t / uint32_t if your array is larger than 255 / 65535 elements.
Also, make sure your arrays don't contain anything else than 0-9.

gf:
For small arrays, two nested loops through the elements may be acceptable. For large/huge arrays, either sort the array in the first place and then find the counts with a single pass through the sorted elements, or set up a number -> count map. This map could be realized e.g. as a hash table, or if the set of possible numbers is a dense range (e.g. 1..9 as given in the example), then it could also be yet another count[] array, indexed by the number.

mariush:
If you only have numbers between 0 and 9, the easiest would be to have a 10 value array and simply count each occurrence. A single for would be enough.

If you have a limited number of values (ex <=32 or <=64) you could use a 32 bit or 64 bit variable to keep track of which value was detected twice, and you could do that by setting individual bits on or off. Then you could have a 32 value or 64 value or (whatever limited amount) value array to keep the count.

If you're memory constrained and the original array doesn't have to remain intact and you have lots of numbers, then an option would be to sort the array, then you can use a simple for loop to go through the sorted array and easily determine how many times each value shows up. You could even keep track of the original position in array by including it in the value ... for example if you have an array with 1000 random values, you know that up to 1024 numbers can be stored in 10 bits  so you could simply shift the original value in the array by 10 bits to the left and then add in those 10 bits the offset in the array, so that after the sort and during sort, you put the ones with smaller offset closer to start of array.
Of course, that's provided your values won't overflow if you shift bits that way.

Kittu20:
I ran code on my system but It doesn't print anything


--- Code: ---#include<stdio.h>

int main()
{

int array[5] = {1, 2, 1, 2, 5 };
int count[10] = {0};

for (int i=0; i<sizeof(array); i++)
{
count[ array[i] ]++;
}
for (int i=0; i<10; i++)
{
printf("%u found %u times\n", i, count[i]);
}

return 0;
}
--- End code ---

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod