I don't know what a unique algorithm is

Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values.

*and of course, I've done a research before posting this, and still haven't understood how it works
Last edited on
closed account (z05DSL3A)
https://en.cppreference.com/w/cpp/algorithm/unique ?
You can combine unique and copy with unique_copy(). Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <iostream>
#include <iterator>

const size_t MAXNOS {20};

int main()
{
	int nos[MAXNOS] {};

	std::cout << "Enter " << MAXNOS << " numbers: ";
	for (auto& n : nos)
		std::cin >> n;

	std::sort(nos, nos + MAXNOS);
	std::unique_copy(nos, nos + MAXNOS, std::ostream_iterator<int>(std::cout, " "));
	std::cout << '\n';
}

Thanks a lot Grey Wolf and seeplus.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <sstream>
#include <iterator>
#include <set>
#include <algorithm>
using namespace std;

istringstream in( "3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4" );

int main()
{
   set<int> S( istream_iterator<int>{ in }, {} );
   copy( S.begin(), S.end(), ostream_iterator<int>{ cout, " " } );
}
Topic archived. No new replies allowed.