Duplicate values in Array

This is a discussion on Duplicate values in Array within the C++ Programming forums, part of the General Programming Boards category; Hey everybody, this is my first post and i'm fairly new at programming in general but i am intersted in ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Duplicate values in Array

    Hey everybody, this is my first post and i'm fairly new at programming in general but i am intersted in it, i was just wondering how i'd be able to go through an array and omit duplicate values in it?

    so far, i've created an array and using a "for" loop, i've allowed the user to input 20 int values to the array, but i'm stuck now, i can't seem to figure out how to omit duplicates and display the values once

    hopefully somebody can help me out?

    thanks in advance,
    Tony

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    1. Hint: If you move to STL classes like list, its a matter of 1 single statment that you can remove duplicates from the list.

    2. For int, you can try out:
    Code:
    int i,j,k,array[20];
    
    for (i=0,j=0; i<20; i++)
    {
     cin >> array[j];
     
     for (k=0;k<j;k++) if (array[k]==array[j]) j--;
     
     j++;
    }
    
    // Now array[0] through array[j-1] contains values with no duplicates
    
    cout << endl << "The array elements:";
    for (i=0;i<j;i++) cout << array[i];
    Hope it helps.

  3. #3
    Unregistered
    Guest
    Do you want to display unique values but allow duplicate values in the array or do you want to prevent duplicates from being entered or do you want to remove duplicates after the first twenty were entered? Do you want to display unique values in ascending or descending or sequence of entry?

    Unfortunately with a language as flexible as C++ comes the responsibility for being as precise as possible with deciding/describing what you want to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  2. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  3. Count distinct array values
    By rkooij in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 04:03 AM
  4. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 07:30 AM
  5. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM

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