> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/01-2024-jan-16/17-all-unique-permutations-of-an-array.md).

# 17 - All Unique Permutations of an array

#### 17. All Unique Permutations of an array

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/all-unique-permutations-of-an-array/1)

![](https://badgen.net/badge/Level/Medium/yellow)

#### My Approach

* Sort the array in ascending order.
* Generate all unique permutations using `std::next_permutation`.
* Return the vector of unique permutations.

#### Time and Auxiliary Space Complexity

* **Time Complexity:** `O(N! * N)`, where N is the length of the array. This is because there are N! permutations, and for each permutation, it takes O(N) time to copy it.
* **Auxiliary Space Complexity:** `O(N!)`, where N is the length of the array. This is the space needed to store all unique permutations.

#### Code (C++)

```cpp
class Solution {
  public:
    vector<vector<int>> uniquePerms(vector<int> &arr, int n) {
        sort(arr.begin(), arr.end());

        vector<vector<int>> out;
        do {
            out.push_back(arr);
        } while (next_permutation(arr.begin(), arr.end()));

        return out;
    }
};
```

#### Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/01-2024-jan-16/17-all-unique-permutations-of-an-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
