tags:

views:

1406

answers:

8

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

please help me to find out this.........

A: 

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.

1800 INFORMATION
+2  A: 
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);
Remember to make sure that your array is sorted before doing this.
Nick Whaley
the last item is not handled. so the singleton case is not handled also.
動靜能量
+1  A: 

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.

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

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.

fastmultiplication
+1  A: 

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.

Mehrdad Afshari
A: 
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));
}
Christoffer
A: 

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

Preston
A: 

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
動靜能量