-3

Here is my array: Arr[8] = {24, 30, 30, 32, 32, 36, 36, 36};

I am trying to eliminate the duplicate numbers from Arr using C++. Any suggestions? Thank you!

10
  • use a <map> datastructure (Hashtable) to filter out the duplicates Commented Jul 5, 2017 at 8:05
  • 3
    Why map? Put them in a set and print them. Commented Jul 5, 2017 at 8:06
  • 1
    std::unique Commented Jul 5, 2017 at 8:06
  • 1
    possible duplicate of question using-a-function-to-remove-duplicates-form-an-array-in-c++ Commented Jul 5, 2017 at 8:06
  • 2
    Map is not essentially hashtable. In MSVC implementation it is red black tree. Moreover, it is complex way. He can simply sort array and remove duplicates by simple ordering Commented Jul 5, 2017 at 8:07

1 Answer 1

2
sort( vec.begin(), vec.end() );
vec.erase( unique( vec.begin(), vec.end() ), vec.end() );
Sign up to request clarification or add additional context in comments.

3 Comments

This is obviously designed for a container such as std::vector, not a C-style array like: int[8].
@Jonas - It works perfectly for a C-style array. Removal from an array involves updating the size. And here the new size is simply unique(...) - arr.
@StoryTeller Yes, I was trying to point out the use of vec.erase (and vec.begin() as oppose to using std::begin(vec)). Either way, this question has to be dube.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.