-1

Can someone give me an algorithm to count distinct elements of an array of integers in one pass.

for example i can try to traverse through the array using a for loop

I will store the first element in another array.And the subsequent elements will be compared with those in the second array and if it is distinct then i will store it in that array and increment counter.

can someone give me a better algorithm than this.

Using c and c++

4
  • What language are you using? Commented Sep 20, 2013 at 12:41
  • What are the elements ? Pointers ? Integers ? Floats ? Commented Sep 20, 2013 at 12:47
  • possible duplicate of Count distinct values in an array - C++ Commented Sep 20, 2013 at 12:48
  • This post has had 486 views till today. Fellow members if you find the question and answers useful please upvote Commented Mar 10, 2016 at 12:37

4 Answers 4

1

Supposing that your elements are integers and their values are between 0 and MAXVAL-1.

#include <stdio.h>
#include <string.h>

#define MAXVAL 50

unsigned int CountDistinctsElements(unsigned int* iArray, unsigned int iNbElem) {
  unsigned int ret = 0;

  //this array will contains the count of each value
  //for example, c[3] will contain the count of the value 3 in your original array
  unsigned int c[MAXVAL];
  memset(c, 0, MAXVAL*sizeof(unsigned int));

  for (unsigned int i=0; i<iNbElem; i++) {
    unsigned int elem = iArray[i];
    if (elem < MAXVAL && c[elem] == 0) {
      ret++;
    }
    c[elem]++;
  }
  return ret;
}

int main() {
  unsigned int myElements[10] = {0, 25, 42, 42, 1, 2, 42, 0, 24, 24};
  printf("Distincts elements : %d\n", CountDistinctsElements(myElements, 10));
  return 0;
}

Output : (Ideone link)

Distincts elements : 6

Sign up to request clarification or add additional context in comments.

4 Comments

why is there a semicolon after the prototype memset
have u given over here the definition of memset
I understood the rest of the code but what is the use of memset
memset initialize all values of c to zero.
0

Maintain a array of structures. structure should have a value and a counter of that value. As soon as you pass an new element in an array being tested create a structure with value and increment the counter by 1.if you pass an existing element in the array then simply access the related structure and increment its counter by 1. Finally after you do a one complete pass of the array, you will have the required result in the array of structures.

3 Comments

that's O(n^2) and hardly one pass, considering you have to loop over your array of structures for every element tested.
why would you increment the counter if u pass an existing element of an array
i asked to count distinct elements
0

Edit: I wasn't aware you wanted just to count the elements. Updated code below.

int countUnique()
{
    uniqueArray[numElements];
    myArray[numElements];
    int counter = 0;
    int uniqueElements = 0;

    for(int i = 0; i < numElements; i++)
    {
       element tempElem = myArray[i];
       if(!doesUniqueContain(tempElem, counter, uniqueArray)//If it doesn't contain it
       {
            uniqueArray[counter] = tempElem;
            uniqueElements++;
       }
    }
    return uniqueElements;
}

bool doesUniqueContain(element oneElement, int counter, array *uniqueArray)
{
    if(counter == 0)
    {
        return false; //No elements, so it doesn't contain this element.
    }
    for(int i = 0; i < counter; i++)
    {
        if(uniqueArray[i] == oneElement)
            return true;
    }
    return false;
}

This is only so you can see the logic

2 Comments

ya but how is this better than using two for loops and doing the same thing u are doing
It isn't. It's still two for loops.
0

How about using a hash table (in the Java HashMap or C# Dictionary sense) to count the elements? Basically you create an empty hash table with the array element type as the key type and the count as values. Then you iterate over your list. If the element is not yet in the hash table, you add it with count 1, otherwise you increment the count for that element.

1 Comment

My first thought was Dictionary as well, but (s)he is using C/C++, and I'm assuming this is for a class as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.