Firstly, it seems that you really want to count int
s, not double
s. It is also very odd to use double
s for your loop indices. Based on these, I'm guessing some person advised you to use double
s 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?