Thread: avoiding duplicates

  1. #1
    Registered User baffleddreams's Avatar
    Join Date
    Aug 2010
    Location
    India
    Posts
    15

    Post avoiding duplicates

    do u noe how 2 avoid duplicates in an array while taking inputs

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Code:
     
    while(!condition)
      enter new number
    
      save to array
    Last edited by Lesshardtofind; 09-19-2010 at 01:52 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For numbers with a small range, make an int array (auxiliary). Say the range was 0 to 1000:
    Code:
    #include <stdio.h>
    #define SIZE 1000
    
    int main() {
      int i, unique, temp; 
      int num[SIZE];
      printf("\n\n");
      for(i=0;i<SIZE;i++)
        num[i] = 0;
    
      i=1;
      while(i) {
        printf("\n Enter a positive integer. First repetition quits: ");
        temp = scanf("%d", &unique);
        getchar();
        if(temp && unique > -1 && unique < SIZE) {
          num[unique]++;
          if(num[unique]>1) {
            printf("\n Repetition detected - terminating loop");
            i=0;
          }
        }
      }
      i=getchar();
    
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }
    For numbers with a large range, you'll have to either check every number in the array.

    Note that the above is approximately the same as getting unique numbers by only allowing numbers from a fixed set or "draw" (which are unique), to be chosen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing duplicates
    By satty in forum C Programming
    Replies: 4
    Last Post: 08-05-2010, 09:33 AM
  2. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  3. Array with at most n duplicates
    By kratz in forum C++ Programming
    Replies: 19
    Last Post: 07-16-2005, 11:46 PM
  4. Replies: 6
    Last Post: 11-28-2004, 11:01 AM
  5. Eliminating duplicates
    By C-Struggler in forum C Programming
    Replies: 3
    Last Post: 03-23-2003, 11:12 PM