Input: arr[] = {1, 1, 2, 2, 2, 3}
Output: arr[] = {1, 2, 3}
Input: arr[] = {1, 2, 5, 5}
Output: arr[] = {1, 2, 5}
Approach 1: Brute Force Approach.
- Create a new array temp[].
- Iterate through the original array arr[].
- For each element, check if it is already present in the new array.
- If not, add it to the new array.
- The new array contains elements without duplicates.
//C++ code to remove duplicate elements from the array #include <bits/stdc++.h> using namespace std; int removeDuplicatesBruteForce(vector<int>& arr) { vector<int> result; for (int i : arr) { //checking if element already exist if (find(result.begin(), result.end(), i) == result.end()) { result.push_back(i); } } arr = result; return result.size(); } int main(){ vector<int> arr = {1, 1, 2, 2, 2, 3, 5}; int n = removeDuplicatesBruteForce(arr); for(int i = 0; i < n; i++){ cout << arr[i] <<" "; } return 0; }
1 2 3 5- Time Complexity: O(n^2) where n is the number of elements in the array.
- Space Complexity: O(n) where n is the number of elements in the array.
Approach 2: Optimized Approach.
- Create a result vector and add the first element of the array.
- Iterate through the original array from the second element.
- For each element, check if it is equal to the previous element.
- If not, add it to the result vector.
- The result vector will now contain elements without duplicates.
//C++ code to remove duplicate elements from the array #include <bits/stdc++.h> using namespace std; int removeDuplicatesOptimized(vector<int>& arr) { vector<int> result; if (!arr.empty()) { //add first element to array result.push_back(arr[0]); for (size_t i = 1; i < arr.size(); ++i) { //check if element is equal to previous element if (arr[i] != arr[i - 1]) { result.push_back(arr[i]); } } } //copy unique elements to original array arr = result; return result.size(); } int main(){ vector<int> arr = {1, 1, 2, 2, 2, 2, 5}; int n = removeDuplicatesOptimized(arr); for(int i = 0; i < n; i++){ cout << arr[i] <<" "; } return 0; }
1 2 5- Time Complexity: O(n) where n is the number of elements in the array.
- Space Complexity: O(n) where n is the number of elements in the array.
Approach 3: Two Pointer Approach.
- Use two pointers - one to iterate through the array and another to keep track of the position to overwrite.
- Iterate through the array from the second element.
- If the current element is different from the previous one, overwrite the next position with the current element.
- The array up to the position of the second pointer will now contain elements without duplicates.
//C++ code to remove duplicate elements from the array //In-place approach #include <bits/stdc++.h> using namespace std; int removeDuplicatesInPlace(vector<int>& arr) { int writeIndex = 1; // Position to overwrite for (size_t i = 1; i < arr.size(); ++i) { if (arr[i] != arr[i - 1]) { arr[writeIndex++] = arr[i]; } } // Resize the array to the size of unique elements arr.resize(writeIndex); return arr.size(); } int main(){ vector<int> arr = {0, 1, 1, 2, 2, 2, 2, 4, 5, 5}; int n = removeDuplicatesInPlace(arr); for(int i = 0; i < n; i++){ cout << arr[i] <<" "; } return 0; }
0 1 2 4 5- Time Complexity: O(n) where n is the number of elements in the array.
- Space Complexity: O(1) in-place modification is performed so no extra space is required.

