Determine duplicates of an Array

Hi, our Computer Science teacher gave us a task to solve, basically we need to verify if there are duplicates in an array, until here it's quite easy. Moreover we should compute trough the program which values are duplicates and how many times they occur. That's where I have problems, I tried to use a nested for loop, but it's not a solution, because a specific number will be considered more than once if it is present more than once and so it will be compared to all elements more than once, giving me a wrong output. I tried to search the net for some solutions, but couldn't find any that was using the simple coding bases I've learned till now. Basically I should work only with array and basic operations, without using string.
I hope my problem is clear and someone can give me a hi

Comments

  • give me a hint*

  • can give me example of input and the output will com please

  • You could sort the array in O(nlog(n)), then simply look until the next number. That is substantially faster than your O(n^2) existing algorithm. The code is also a lot cleaner. Your code also doesn't ensure no duplicates were inserted when they were re-entered. You need to prevent duplicates from existing in the first place.

    std::sort(userNumbers.begin(), userNumbers.end());
    for(int i = 0; i < userNumbers.size() - 1; i++) {
    if (userNumbers[i] == userNumbers[i + 1]) {
    userNumbers.erase(userNumbers.at(i))
    i--;
    }
    }

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories