Any pro can find the solution?

Write a program with a function int distinct(int *p, int n);

where the parameter pointer p is the base address of an array of size n. The function returns the number of distinct (unique) elements in the array.

E.g., suppose array x has contents {1,6,4,6,1}. Then the call distinct(x, 5) should return 3. (The three distinct elements are 1,6,4.)
Stick them in a set<int>.

Return the size of the set.
Just to hijack yet another thread :-) may I ask if there’s a method to determine which of these two options is faster (or let’s say more efficient) without testing them?

std::set<int> + size()
vs
std::unordered_set<int> + bucket_count()
unordered_set should generally be faster since it uses a hash table instead of a balanced binary tree.
But you should still use size() if you want the number of elements it contains.
Thank you, dutch!
Topic archived. No new replies allowed.