0

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

1=4
2=3
3=2
4=1
4
  • A few questions: What is the range of values in input array? How many elements can be in input array? Output should be sorted out (like in example)? Commented May 12, 2009 at 8:30
  • How are array values limited? Commented May 12, 2009 at 8:32
  • Is the input array always sorted as in your example? Commented May 12, 2009 at 8:37
  • What should the output be if the input is {1,1,1,2,2,3,1,1,2}? Commented May 12, 2009 at 14:12

8 Answers 8

3
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);
Sign up to request clarification or add additional context in comments.

2 Comments

Remember to make sure that your array is sorted before doing this.
the last item is not handled. so the singleton case is not handled also.
2

if it's already sorted, there may be a faster way

Split the list into halves. deal with each section in this way: test the first and last nubmer. If they're the same, you know the result. if it's not the same, split it in the middle and recurse over each half again.

With long runs of the same number, this will be efficient. It should revert to the one-by-one method if a section is small. You could sample the data at random spots, testing s, s+1, to get a percentage of the time a number increases from its predecessor. The higher than number is, the earlier you should switch to one-by-one method.

This method is also parallelizable.

Comments

1

It seems the array is sorted (According to the example). If so you just have to pick the first element and iterate through the array until a distinct value is found. Within this process you can have a counter within loop to count the occurences.

Then pick the found distinct value instead of first element and repeat the process.

2 Comments

And if the array isn't sorted in first place, then just sort it.
Yes, Select some sorting algorithm according to the nature of your distribution and apply that if the series is not sorted.
1

If the range of values are small, you can have another array holding the count of each element, pretty much like counting sort. If the range of values is large, you'll need a hash table.

Comments

0

A good method is to iterate through the elements in the array and count up how many of each one that you see. Once you have finished, you print out the counts that you got.

Comments

0
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));
}

Comments

0

Easy example explained.

// Count each element create a sorted hash to hold each unique element from the original array (could also be done as a linked list) read each element from the array if the hash element already exists increase the count (value) of that key if the hash element does not exist create a key value pair (value = 1) loop

// Print each element loop through the key's of the hash and printf("%d: %d\n", key, value); if you need to represent zero values as well, implement a lastKey and do a key to lastKey comparison to determine if a key was zero

Sort and simple program/function

Comments

0

handled:
1) end of array item
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 the 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 

Comments