Question: Please write in C++ Given a list of random numbers, remove all duplicates from the list and return a list of only unique numbers. You
Please write in C++
Given a list of random numbers, remove all duplicates from the list and return a list of only unique numbers. You may not use a for-loop to solve this problem.
Starter Code:
#include#include #include #include static int getRandomNumber(); template std::ostream& operator<<(std::ostream& out, std::vector list) { out << "["; for (const auto& val : list) { out << val << ", "; } out << "]"; return out; } int main() { std::vector list; std::generate_n(std::back_inserter(list), 30, []() { return getRandomNumber(); }); std::cout << list << " "; // TODO: remove duplicates from the list and print out the result // NOTE: no for loops, use algorithms } static int getRandomNumber() { static std::mt19937 rng{ std::random_device{}() }; static std::uniform_int_distribution rand{ 0, 20 }; return rand(rng); }
Sample Output:
[16, 1, 9, 4, 16, 7, 12, 10, 18, 13, 0, 14, 18, 13, 17, 18, 4, 14, 0, 14, 17, 18, 13, 19, 5, 18, 0, 3, 17, 15, ] [15, 3, 5, 19, 17, 14, 0, 13, 10, 18, 7, 4, 9, 12, 1, 16, ]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
