Check that no numbers are equal

This is a discussion on Check that no numbers are equal within the C++ Programming forums, part of the General Programming Boards category; Hi, OK, I have an integer array with 5 numbers in it. How would I check that none of these ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Check that no numbers are equal

    Hi,

    OK, I have an integer array with 5 numbers in it. How would I check that none of these numbers equal each other. I thought about using an if statement, but it started getting very long and complicated. Does anyone have any ideas about how I could do this.

    Thanks,
    David

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For a large array the most efficient way is to sort them and then compare adjacent elements. Check out sort() to see if you think it's worthwhile.

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I think that is what I want, but I am having trouble grasping how to use the augments of sort. They are iterator start, and iterator end. Could you please help me with this?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Actually, come to think of it, the best way all around with just 5 elements is a double loop. So if you have an array a[5],
    Code:
    for (int i=0; i<5; i++) {
      for (int j=i+1; j<5; j++) {
        if (a[i] == a[j]) {
          //equal
        }
      }
    }
    Of course for an array of any length N, you could do the same thing. The only problem is that the time is O(N^2). The sorting method gives time O(N log N). Of course, N=5 is small enough it's not worth bringing all the overhead of sort() and it's probably faster to just use the double loop. But if you wanted to do it, it would just look like
    Code:
    #include <algorithm>
    
    int main() {
      int a[5] = {3,1,8,4,2};
      std::sort(a, a+5);
      for (int i=0; i<4; i++) {  // 4 == 5-1
        if (a[i] == a[i+1]) {
          // equal
        }
      }
    }

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Ohh, thanks! I understand the double loop. I still don't understand sort, but it's not important right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 01:18 AM
  2. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 10:56 PM
  3. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  4. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  5. check for numbers
    By Unregistered in forum C Programming
    Replies: 20
    Last Post: 07-04-2002, 12:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21