1

I'm trying to print similar numbers using a loop, but I can't figure out how to check all the duplicate characters in a vector, how can I solve this?

int main() {
    vector <double> n0, similar;

    cout << "Enter numbers: ";
    for(double temp; cin >> temp;)
        n0.push_back(temp); // stop using ctrl+d...

    const double smallest = *min_element(n0.begin(),n0.end());

    const double biggest  = *max_element(n0.begin(),n0.end());

    cout << "\nVector: ";
    for(double x = 0; x < n0.size(); ++x){
        cout << n0[x]  << " ";
        for(double z = -1; z < n0.size(); ++z){
            if (n0[z] == n0[x]){
                similar.push_back(n0[z]);
            }
        }
    }

    cout << "\nSimilar: ";
    for(double v = 0; v < similar.size(); ++v){
        cout << similar[v] << " ";
    }
    cout << '\n' << "Smallest: " << smallest
        << '\n' << "Biggest: " << biggest << '\n';
}
schorsch312
  • 5,553
  • 5
  • 28
  • 57
f0xtr0d
  • 37
  • 7

1 Answers1

2

Firstly, it seems that you really want to count ints, not doubles. It is also very odd to use doubles for your loop indices. Based on these, I'm guessing some person advised you to use doubles for all kinds of number. That person is wrong.

Now to your question - it seems you want to loop reading numbers from the input, then at the end print out any that occurred more than once, while also printing the min and max inputs. For tracking the presence of elements, a hash-based associative container is a nice choice, because they support fast (amortised constant in the average case) insertion and lookup based on the key, with a very convenient interface.

There are various ways to use such a container, this is just one way:

#include <algorithm>
#include <iostream>
#include <limits>
#include <unordered_map>

int main()
{
    std::unordered_map<int, int> occurrences; // map inputs to the number of times they occurred
    int min = std::numeric_limits<int>::max();
    int max = 0;

    int inp;
    while (std::cin >> inp) { // read and immediately check stream state for success
        // first update min and max
        min = std::min(min, inp);
        max = std::max(max, inp);

        // update count
        // note that the first time we see a particular input, the map will create a default value of 0
        ++occurrences[inp];
    }

    std::cout << "min: " << min << ", max: " << max << '\n';
    std::cout << "duplicates:\n";
    for (auto const& p : occurrences) { // the map gives you pairs of (int, int)
        if (p.second > 1) {
            std::cout << p.first << '\n';
        }
    }
}

This prints:

$ echo "1 2 4 1 4 5 7 4 1 4 5" | ./SO                                                                                                                 
min: 1, max: 7
duplicates:
5
4
1

Note that it prints the duplicate entries, but not in any guaranteed order. If you need the duplicates printed in a specific (probably ascending) order, you'll need to do something different. Possibilities include putting the duplicates into another container and sorting it, or using the sorted equivalent of the map in the first place (though it has different, usually worse, performance characteristics).

You might also want to consider using unsigned int throughout, as it seems you don't want to allow negative numbers?

BoBTFish
  • 19,167
  • 3
  • 49
  • 76