How to Convert an array to set in C++?

In this article, we will discuss how to convert an array to a set in C++.

Table Of Contents

Problem Description

Here we are given an array and we have to push it’s values in a set.

Input

int arr[] = {12, 56, 823, 7, 1023};

Push the values in array arr to a set. If we print the set s then output should be

7 12 56 823 1023

As set keeps the element in sorted order. Therefore, numbers will be printed in sorted order. There are different ways to convert an array to a set in C++. Let’s see all techniques one by one,

Convert an array to Set by pushing elements one by one

In this method, we will iterate over the whole array and add each element to set.

Steps are as follows:

  1. Iterate over the whole array.
  2. During iteration, push every element into set.
  3. Print the set.

Time Complexity: O(n)
Space Complexity: O(n)

Example

// C++ program to convert an array into a set
#include <iostream>
#include <set>

using namespace std;

// Driver Code
int main()
{
    int arr[] = {12, 56, 823, 7, 1023};

    // n is the size of array
    int n = sizeof(arr) / sizeof(arr[0]);

    set<int> s;

    // filling the array values in the set
    for(int i=0; i<n; i++)
    {
        s.insert(arr[i]);
    }


    cout<<"Value of Set are \n";

    // Print the value
    for(auto it: s )
    {
        cout<< it << " ";
    }
    cout << endl;

    return 0;
}

Output

Value of Set are 
7 12 56 823 1023

In this example we converted an array to a set in C++.

Convert an array to Set using Range Constructor

In this set constructor, we can pass a range of elements that needs to be copied to the set. In this method we reduced the space complexity of above method. If we dont want to copy the entire array, then we can also specify the range of elements.

Time Complexity: O(n)
Space Complexity: O(n)

Here, n is the range till where we want to copy the elements from array to set.

Example

// C++ program to convert an array into a set
#include <iostream>
#include <set>

using namespace std;

// Driver Code
int main()
{
    int arr[] = {12, 56, 823, 7, 1023};

    // n is the size of array
    int n = sizeof(arr) / sizeof(arr[0]);


    // n is the size but in below formula
    // we can take any value from 1 to n 
    set<int> setObj(arr, arr+n);

    cout<<"Value of Set are \n";

    // Print the value
    for(auto it: setObj )
    {
        cout<<it<<" ";
    }
    cout<< endl;

    return 0;
}

Output

Value of Set are 
7 12 56 823 1023 

In this example we converted an array to a set in C++.

Convert an array to Set using begin() and end()

In this method, we use STL function to convert begin() and end() to convert pointers to iterators. Then we will pass the iterators to set constructor to convert an array to a set.

Time Complexity: O(n)
Space Complexity: O(n)

Example

// C++ program to convert an array into a set
#include <iostream>
#include <set>

using namespace std;

// Driver Code
int main()
{
    int arr[] = {12, 56, 823, 7, 1023};

    // Convert array to set
    set<int> setObj(begin(arr), end(arr));

    cout<<"Value of Set are \n";

    // Print the value
    for(auto it: setObj )
    {
        cout<<it<<" ";
    }
    cout<<endl;

    return 0;
}

Output

Value of Set are 
7 12 56 823 1023 

In this example we converted an array to a set in C++.

Summary

We have seen three different methods to convert an array into a set. One is a naive solution where time and space complexity is O(n). Then another method we reduced the space-time complexity and the third is using STL functions. The second and third method look similar but they are different because in the second method we can take as a range but in the third method, we don’t have this option it will copy the entire array. We can see every method has its own time complexity and space complexity.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top