Displaying only unique values in array

I am trying to write a program. It reads 5 integers between 10 and 100. As each number is read, the program validates it and stores it in the array. Then it displays the values.
I am trying to add some extra to the program. It should only store the values in the array if it is not the duplicate of a number already read. Then display only the unique numbers.

Can I get some help about how can I store and display only the unique numbers please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <array>

using namespace std;

//void duplicate(
    
int main(){
  int temporary{0};
  const size_t arraySize{5};
  array<int, arraySize> array1{0};
  
  //loop to read 5 numbers
  for(size_t x{0}; x<array1.size(); ++x){
      cout << "Please enter 5 numbers between 10 and 100" << endl;
      
      //store the entered number in a temporary variable
      cin >> temporary;
      
      //validate it and store it in the array
      if(temporary > 10 && temporary < 100){
          array1[x] = temporary;
      }
      else{
          cout << "invalid value" << endl;
      }
  }
  
  //display the array
  for(size_t x{0}; x<array1.size(); ++x){
      cout << array1[x] << endl;
  }
}
Before you add it to the array use std::find to check if it is in the array already.
The problem is the temporary variable will be lost each time the program loops so I can`t use the std::find.
Use a std::set<int>, which stores only unique elements.
When its size reaches 5 then you can either use it as your container or put the contents somewhere else.
The problem is the temporary variable will be lost each time the program loops so I can`t use the std::find.

Of course you can. std::find receives a copy of the temporary input.
Anyway, I think @lastchance's idea is much better.
The problem is the temporary variable will be lost each time the program loops so I can`t use the std::find.


Yes you can. If you use a std::array, that is how you do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <array>
#include <algorithm>

using namespace std;

int main()
{
	const size_t arraySize {5};
	array<int, arraySize> array1 {};

	cout << "Please enter " << arraySize << " unique numbers between 10 and 100\n";

	for (size_t x {}; x < array1.size(); ) {
		int temporary {};

		cin >> temporary;

		//validate it and store it in the array
		if (temporary > 10 && temporary < 100)
			if (std::find(array1.begin(), array1.end(), temporary) != array1.end())
				cout << "Duplicate value\n";
			else
				array1[x++] = temporary;
		else
			cout << "Invalid value\n";
	}

	cout << '\n';
	for (const auto a : array1)
		cout << a << '\n';
}


Or using a set as per lastchance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <set>

using namespace std;

int main()
{
	const size_t arraySize {5};
	set<int> array1;

	cout << "Please enter " << arraySize << " unique numbers between 10 and 100\n";

	while (array1.size() != arraySize) {
		int temporary {};

		cin >> temporary;

		//validate it and store it in the array
		if (temporary > 10 && temporary < 100) {
			if (!array1.insert(temporary).second)
				cout << "Duplicate value\n";
		} else
			cout << "Invalid value\n";
	}

	cout << '\n';
	for (const auto a : array1)
		cout << a << '\n';
}

Last edited on
Thank you for your help. It`s very useful.
if (std::find(array1.begin(), array1.end(), temporary) != array1.end())
cout << "Duplicate value\n";


I am just familiarising with the std::find function.
I have a question about it. It is clear between the parentheses but the
!= array1.end()
is not. Why is only the last value is important if we are looking for a value?
std::find is a function. A function that returns a value. What value does it return?
See http://www.cplusplus.com/reference/algorithm/find/
The answer is:
Returns an iterator to the first element in the range [first,last) that compares equal to val.
If no such element is found, the function returns last.


In the call std::find(array1.begin(), array1.end(), temporary) the last is array1.end().
Therefore, the value (temporary) is unique only if the std::find returns array1.end().

Furthermore, the last is after the last element. "Outside" of the array. Its value is never looked at.
Last edited on
Topic archived. No new replies allowed.