Lesson 11 of 24

STL Algorithms: sort, reverse and find

10 min 30 XP

<algorithm> gives you ready-made sort, reverse, max_element, min_element, count, find and binary_search; <numeric> adds accumulate. One line, lots of work done.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

sort() runs in O(n log n). binary_search only works on a sorted range.

Challenge

+30 XP

Read n and n numbers. Print the distinct values in ascending order, then on the next line how many distinct values there are. Input: 7 4 1 4 2 1 9 2 Output: 1 2 4 9 4

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.