1

Possible Duplicate:
How can I remove duplicate values from a list in c++?

Hi, I have to remove duplicate values from a list in c++. can any one tell me how to do that as I am new to c++.

Any sample code will be highly appreciated.

Regards Shekhar

Community
  • 1
  • 1
Shekhar
  • 49
  • 1
  • 1
  • 3
  • 3
    duplicate http://stackoverflow.com/questions/4877504/how-can-i-remove-duplicate-values-from-a-list-in-c – Marco Mustapic Feb 03 '11 at 04:01
  • 1
    You've never voted or accepted an answer. Your lack of karma will turn-off a lot of people who might otherwise help you. – chrisaycock Feb 03 '11 at 04:01
  • Please read http://stackoverflow.com/faq#howtoask and do not ask the same question twice. – johnsyweb Feb 03 '11 at 04:06
  • @chrisaycock: If he didn't accept answers, it's because he doesn't know why it's necessary to do it. Mentioning it as a 'lack of karma' won't make any sense to him. I believe he's a polite and respectful person. He just needs an explanation of how SO works. – Nav Feb 03 '11 at 04:11
  • @Shekhar: Please understand that people are investing a lot of time to help you by answering your questions. Since you can't pay people for their effort, the one thing you can do to honour people is to up-vote any answers which are helpful to you. Just click the grey triangle pointing upward. For the answer that was most helpful to you, you can also click the tick mark that's below the grey triangles. Clicking these gives people additional virtual points and medals. Nice to have you here. – Nav Feb 03 '11 at 04:16
  • @Nav : I m new to this forum and was not knowing about it. Thanks a lot for giving this useful information – Shekhar Feb 03 '11 at 09:18
  • @Shekhar: Nice of you to respond positively. Marking answers as "Accepted answers" is also important. See the 'tick' mark beside all answers? You've got to click the tick mark of the answer that you accept as the best answer to your question. This is what chrisaycock was mentioning. The practice is important because Stack Overflow keeps track of answered questions. See this: http://blog.stackoverflow.com/wp-content/uploads/04pctanswered.png . I suggest you go back to all your questions and click the tick mark to the best answers. Doing all this will give you extra points as well. – Nav Feb 03 '11 at 10:45

5 Answers5

4

std::sort, then std::unique

a1ex07
  • 36,826
  • 12
  • 90
  • 103
2

I think you want STL unique (and some example code as requested).

Raph Levien
  • 5,088
  • 25
  • 24
0

Check this:

http://www.java2s.com/Tutorial/Cpp/0340__list/DemonstratingtheSTLlistuniquefunctions.htm

As said by Raph, use stl unique. as simple as that.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
Arunmu
  • 6,837
  • 1
  • 24
  • 46
0

I haven't made much usage of lists but I envision them to be something like arrays. In that case, one way to do this is to sort them alphabetically and then iterate through the list compare the current element to the next element. If they're the same delete. If not, proceed!

Paul Calabro
  • 1,748
  • 1
  • 16
  • 33
  • http://www.cplusplus.com/reference/stl/list/unique/ will do the job, just remember the list has to be sorted because it IS actually doing that compare and contrast delete operation like i mentioned. – Paul Calabro Feb 03 '11 at 04:03
0

The answer to your question is to used STL function name "Unique", but this function requires the collection to be sorted, so if your collection is not sorted then use STL Sort.

Jame
  • 21,150
  • 37
  • 80
  • 107