Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,912 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,555 people online right now. Registration is fast and FREE... Join Now!




c++ removing duplicates from an array

 
Reply to this topicStart new topic

c++ removing duplicates from an array

sorikorik
11 May, 2008 - 05:37 PM
Post #1

New D.I.C Head
*

Joined: 11 May, 2008
Posts: 1

i've been working on this for around 2 days and i can't find out how to do it. i only know how to set an "auto" code which is something like this:

CODE
#include <ext/hash_set>
#include <iostream>
using namespace std;

int main()
{
    int hold;
    typedef __gnu_cxx::hash_set<int> TyHash;
    int data[] = {1, 2, 3, 2, 3, 4};

    TyHash unique_set(data, data + 6);

    cout << "Set items:" << endl;
    for (TyHash::iterator iter = unique_set.begin(); iter != unique_set.end(); iter++)
          cout << *iter << " ";
    cout << endl;
    cin >> hold;
}


but i want it to be able to ask stuff, like this:
Please enter 10 integers, hitting return after each one:
5
75
10
75
5
80
10
5
5
50
You entered 5 distinct numbers:
5 75 10 80 50


User is offlineProfile CardPM
+Quote Post

skaoth
RE: C++ Removing Duplicates From An Array
11 May, 2008 - 07:40 PM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
You can simply use the stl::set object

Remember to #include <set>
CODE

....
int ary[] = {1, 50, 100, -1, 5,  1, 44, 100 };
int arySize =  sizeof(ary) / sizeof(int);
    
std::set<int> mySet;
    
for(int i = 0; i < arySize; i++)
{
    mySet.insert(ary[i]);
}

std::set<int>::iterator itr;
for(itr = mySet.begin(); itr != mySet.end(); itr++)
{
    std::cout << *itr << " ";
}
std::cout << std::endl;


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:48AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month