C++ Program to Convert Array to Set (Hashset)


The array is a data structure that is available in C++ and is used to hold a sequential collection of elements of the same type. An array has a size that is fixed, but can be expanded or shrank if needed. It is important to think of an array as a collection of variables of the same type even though it is used to store a collection of data. Sets, or in this case unordered sets are a container that stores elements of a particular datatype in any order. A hash table is used to implement an unordered_set where keys are hashed into hash table indices to help ensure that the insertion is always random.

Conversion from an array to a unordered set can be done using various methods which we have discussed further.

Inserting array elements one by one into the set

The easiest way of converting an array to a unordered set is by using a for loop and inserting each array element individually into the unordered set. We take a look at the syntax and the algorithm next.

Syntax

int ip[] = <integer array>;
   unordered_set<int> op;
   for( int i : ip) {
      op.insert(i);
}

Algorithm

  • Take input in an integer array ip.
  • Define an unordered_set op.
  • For each element i in the array ip, do −
    • Insert ip into op.
  • Display the contents of op.

Example

#include <bits/stdc++.h> using namespace std; template <size_t N> unordered_set<int> solve( int (&ip)[N] ) { //an unorderd set is declared unordered_set<int> op; //each element is inserted using insert function for(int i : ip) { op.insert(i); } return op; } int main() { int ip[] = {50, 80, 90, 40, 30}; unordered_set<int> op = solve(ip); //display the input cout<< "The input array is: "; for(int i : ip) { cout<< i << " "; } //display the output cout<< "\nThe output set is: "; for(int j : op) { cout<< j << " "; } return 0; }

Output

The input array is: 50 80 90 40 30 
The output set is: 30 40 90 50 80

We have declared an integer array ip and iterated through all the elements in the array. We declared the output set as op and inserted each element into the unordered set using the insert function available within the container. The result we can see is an unordered set of values that are also present in the array.

Constructing the set using the range sconstructor

An unordered_set can also be created using its range constructor. A range constructor takes two inputs; the starting pointer of the input array and the size of the input array added with the starting pointer.

Syntax

int ip[] = ;
int n = sizeof(ip) / sizeof(ip[0]);
std::unordered_set op(ip, ip + n);

Algorithm

  • Take input in an integer array ip.
  • Determine the size of the input array using sizeof operators.
  • Assign the size of the array to an integer variable n.
  • Construct an unordered_set op using the array starting pointer and the size of the array.
  • Display the contents of op.

Example

#include <bits/stdc++.h> using namespace std; template <size_t N> unordered_set<int> solve(int (&ip)[N]) { //the size is determined of the input array int n = sizeof(ip) / sizeof(ip[0]); //output set is constructed using range constructor std::unordered_set<int> op(ip, ip + n); return op; } int main() { int ip[] = {30, 20, 50, 10, 70}; unordered_set<int> op = solve(ip); //display the input cout<< "The input array is: "; for(int i : ip) { cout<< i << " "; } //display the output cout<< "\nThe output set is: "; for(int j : op) { cout<< j << " "; } return 0; }

Output

The input array is: 30 20 50 10 70 
The output set is: 70 10 50 20 30

In this example, we had to determine the size of the array using the sizeof functions. We assign the size to a variable n and use the pointer ip and ip + n to create the unordered_set op.

Conclusion

The unordered_set is able to contain any type of data. To change the type of data it holds, we have to change the datatype that is enclosed within the <>. Primitive and user-defined types both are well supported in this container. In practise, unordered_set work quite well and typically offer a constant time search operation. All operations on the unordered_set typically take constant time O(1), although in the worst case they can take up to linear time O(n) depending on the internal hash function.

Updated on: 19-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements