Thread: Unique function in C

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    Unique function in C

    Hi everyone and Merry Xmas
    I'm looking for a C function like "Unique" function in Matlab to eleminate repetition in an array. For exemple my array is:
    Code:
    T=[0.88 0.3 0.3 0.4 0.6 0.88];
    and the result will be:
    Code:
    T=[0.88 0.3 0.4 0.6];
    Thanks a lot

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    Rome
    Posts
    7
    There are no functions that works as Unique
    but it's not a difficult exercise

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you're using C++, I think there's a function which will do what you want in <algorithm>, but it's not easy to use. You're best off writing it yourself.

    A simple way to do this is to sort the array, and then just skip over sequences of the same element in the array.

    Be forewarned that comparing floating point numbers exactly with == may not always do what you expect, due to floating-point inaccuracies. It's usually better to use something like
    Code:
    int doubles_equal(double x, double y) {
        return fabs(x - y) < 0.0001;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    Thank you for your idea, I found this program in c++, but it's for integer and not for real values, but I don't understand the use of pointer


    Code:
    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    using namespace std;
    int main(void)
    {
        // Construit un tableau de 10 entiers :
        int t[10] = { 1, 2, 2, 3, 5, 2, 4, 3, 6, 7 };
        // Supprime les doublons :
        int *fin = unique(t, t+9);
        // Affiche le tableau résultat :
        int *p = t;
        while (p != fin)
        {
            cout << *p << endl;
            ++p;
        }
            return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    What i can suggest is that, you are having any array of integers......


    traverse the loop

    like ........


    from 0 to n

    from 1 to n

    if values are equal eliminate one of the index value and adjust the array.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hiba, what is the total range of your floats?

    If it is not large, there is a very simple way to do this.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    the floats vary between 0 and 1, and my array may have at least 200 values

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Hiba View Post
    the floats vary between 0 and 1, and my array may have at least 200 values
    What is the precision value between 0 to 1

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    One way of doing it:

    1. make new array to store unique values
    2. for each value in original array:
    if it's not in the unique array, add it to the unique array, else ignore it.

    Probably a faster/better way to go about it, but that's probably the most straightforward way to do it.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This was what I was thinking of:

    Make an array of int's, (I'll call it haveIt[]), big enough to span the range of the float array values.

    For each entry in the float array, multiply the value by 100, and put the result into the haveIt array:

    float value[] = {0.03, 0.82, 0.03}
    int haveIt[] would then have haveIt[3] = 1 and haveIt[82] = 1, and all the rest of haveIt[] would still be set to zero.

    Code:
    if(haveIt[(value[n] * 100)])  //test to see if the number in value[n] is unique or not
      printf("\n Not unique!");
    else
      printf("\n That's unique!");
    The advantage of this distribution count is that it's VERY fast, just an array look up. The disadvantage is that it won't work if the values span a large range of numbers.

    Here's another way:

    again, make an int array, sized to your call it index[]. Initialize it like so:
    Code:
    for(i = 0; i < numValues; i++)
      index[i] = i;
    which looks silly, but you'll see it's a great programming trick.

    Now use the index array to sort your values array. This is a simple sort:

    Code:
    for(i = 0; i < numValues -1; i++) {
      for(j = i+1; j < numValues; j++) {
         if(values[index[i]] < values[index[j]] {
            temp = index[i];
            index[i] = index[j];
            index[j] = temp;
         } 
      }
    }
    Now the original values have not been moved at all, but you can see them in sorted order, by going through the indeces:

    Code:
    for(i = 0; i < numValues; i++)
      printf("%8d", values[index[i]];
    Now that we have the values in sorted order through
    the index, we can also do a binary
    search, to find out if we have a duplicate value, or not.

    Code:
    for(i = 0; i < numValues; i++)
      binarySearch(values, values[i]);
    A binary search is simply the old "guess a number" game
    you played as a kid, with a smart alecky neighbor kid:

    You keep cutting the space you need to search, in half.

    The advantages are that the original values are not moved at all, and it's also fast (not as super fast as the first one, but still fast.

    Learning to sort through an index is one of the great tricks of programming, and so is the binary search.

    if you want to go this route, let me know if you need help with the binary search function. It's simple.

    All the above code is off the cuff, and untested. Consider it pseudo-code. Any problems, and I'll be glad to correct it.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A point of information: in the C++ example that you found, the std::unique generic algorithm was used. This algorithm makes a single pass over a sorted range to "remove" duplicates. (Strictly speaking, it does not have to operate on a sorted range, but that is how it would be done to remove all duplicates.)

    If you use Adak's second suggestion, then you likewise have a sorted range, and thus you can apply the same single pass algorithm to remove duplicates, instead of using binary search (but you have to implement it yourself since there is no std::unique equivalent in the C standard library). As dwks mentioned, care must be taken when comparing floating point numbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM