Wait! Did we interpret this correctly:
the output should be 2 since 38 is repeated and 40 too |
The input had
three unique values: 37, 38, and 40.
The input had 1*37, 3*38 and 2*40, a total of six values.
Only
two unique values occur
more than once: 38 and 40.
Is it actually the last detail that you seek?
@
sujitnag:
std::set contains only the unique values and thus your count is "1" every time.
MartinMorcos wrote: |
---|
how to sort the values first ? O.o
and for the other way i know that but cant do it as a code |
You grasp the principle but not the syntax of the language. There are still two levels in the syntax of the language.
1. To "use C++" you would exploit the standard library. For example:
1 2 3
|
// The count of unique values in array M
std::set<int> st1( M, M+N );
std::cout << st1.size() << '\n';
|
For sorting, there is
std::sort
.
For histogram, there is that
std::map
. If we just want to know whether a value repeats, but do not care how many times, then I would use
std::map<bool>
:
IF map does not have key V, then insert (V,false)
ELSE map has key V and we update map[V] to true
Count the elements of map that have value true |
2. "Learn". Rather than using the library data structures and algorithms, you implement them with more primitive constructs and train logical thinking while doing it.
For example, you had:
1 2 3 4 5 6 7
|
int N;
int M[500]; // This may be too much or not enough
if ( cin >> N ) { // N is a number, but is it within [0,500]?
for (int i = 0; i < N; i++){
cin >> M[i]; // Does the read succeed?
}
}
|
sujitnag had:
1 2 3 4 5 6
|
std::vector<int> ca;
int inp;
// no size of data is asked first, but one needs EOF or non-integer to indicate the end of data.
while( cin>>inp ) { // success of each input is checked
ca.push_back(inp);
}
|
The versions do differ in details.
The plain array is static and sets limits at compile-time. "primitive"
The vector dynamically resizes in order to be big enough for the data and has methods like vector::size() to check how many elements it has, i.e. the N is within the vector object, not as separate variable.