Given a sorted array arr[] of size n, the goal is to rearrange the array so that all distinct elements appear at the beginning in sorted order. Additionally, return the length of this distinct sorted subarray.
Note: The elements after the distinct ones can be in any order and hold any value, as they don't affect the result.
Examples:
Input: arr[] = [2, 2, 2, 2, 2]
Output: [2]
Explanation: All the elements are 2, So only keep one instance of 2.Input: arr[] = [1, 2, 2, 3, 4, 4, 4, 5, 5]
Output: [1, 2, 3, 4, 5]
Input: arr[] = [1, 2, 3]
Output: [1, 2, 3]
Explanation : No change as all elements are distinct.
Table of Content
[Naive Approach] Sorted Set - Works for Unsorted Also - O(n) Time and O(n) Space
- Traverse the sorted array and insert each element into a set.
- A set does not allow duplicate values, so repeated elements are automatically removed.
- Finally, convert the set into an array/list and return it.
#include
#include
#include
using namespace std;
vector<int> removeDuplicates(vector<int>& arr) {
// Put all elements in a sorted set
set<int> st(arr.begin(), arr.end());
// Put set elements into a result array
return vector<int>(st.begin(), st.end());
}
int main() {
vector<int> arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
vector<int> uniqueArr = removeDuplicates(arr);
for (int i = 0; i < uniqueArr.size(); i++)
cout << uniqueArr[i] << " ";
return 0;
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeSet;
import java.util.Set;
public class Main {
public static ArrayList<Integer> removeDuplicates(int[] arr) {
// Put all elements in a sorted set
Set<Integer> st = new TreeSet<Integer>();
for (int num : arr) {
st.add(num);
}
// Put set elements into a result ArrayList
ArrayList<Integer> result = new ArrayList<Integer>(st);
return result;
}
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
ArrayList<Integer> uniqueArr = removeDuplicates(arr);
for (int i = 0; i < uniqueArr.size(); i++) {
System.out.print(uniqueArr.get(i) + " ");
}
}
}
from typing import List
def removeDuplicates(arr: List[int]) -> List[int]:
# Put all elements in a sorted set
st = set(arr)
# Put set elements into a result array
return sorted(list(st))
if __name__ == "__main__":
arr = [1, 2, 2, 3, 4, 4, 4, 5, 5]
uniqueArr = removeDuplicates(arr)
for i in uniqueArr:
print(i, end=' ')
using System;
using System.Collections.Generic;
public class MainClass {
public static List<int> removeDuplicates(int[] arr) {
// Put all elements in a sorted set
SortedSet<int> st = new SortedSet<int>();
foreach (int num in arr) {
st.Add(num);
}
// Put set elements into a result List
List<int> result = new List<int>(st);
return result;
}
public static void Main(string[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
List<int> uniqueArr = removeDuplicates(arr);
for (int i = 0; i < uniqueArr.Count; i++) {
Console.Write(uniqueArr[i] + " ");
}
}
}
function removeDuplicates(arr) {
// Put all elements in a sorted set
let st = new Set(arr);
// Put set elements into a result array
return Array.from(st).sort((a, b) => a - b);
}
let arr = [1, 2, 2, 3, 4, 4, 4, 5, 5];
let uniqueArr = removeDuplicates(arr);
for (let i = 0; i < uniqueArr.length; i++) {
console.log(uniqueArr[i] + ' ');
}
function removeDuplicates($arr) {
// Put all elements in a sorted set
$st = array_unique($arr);
// Put set elements into a result array
sort($st);
return $st;
}
$arr = [1, 2, 2, 3, 4, 4, 4, 5, 5];
$uniqueArr = removeDuplicates($arr);
foreach ($uniqueArr as $i) {
echo $i. ' ';
}
?>
Output
1 2 3 4 5
[Expected Approach] - Single Traversal - O(n) Time and O(1) Space
Since the array is sorted, all occurrences of an element would be consecutive.
We mainly need to check if the current element is same as the previous element or not.
Step by step implementation:
- Loop through the array for i = 0 to n-1. At each index
i, ifarr[i]is different fromarr[i-1], add it to the result. - After the loop, the result contains the unique element.
#include
#include
using namespace std;
vector<int> removeDuplicates(vector<int>& arr) {
int n = arr.size();
if (n <= 1)
return arr;
// Start from the second element
int idx = 1;
vector<int> result;
result.push_back(arr[0]);
for (int i = 1; i < n; i++) {
if (arr[i]!= arr[i - 1]) {
result.push_back(arr[i]);
}
}
return result;
}
int main() {
vector<int> arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
vector<int> result = removeDuplicates(arr);
for (int i = 0; i < result.size(); i++)
cout << result[i] << " ";
return 0;
}
import java.util.ArrayList;
public class Main {
public static ArrayList<Integer> removeDuplicates(int[] arr) {
int n = arr.length;
ArrayList<Integer> result = new ArrayList<>();
result.add(arr[0]);
for (int i = 1; i < n; i++) {
if (arr[i]!= arr[i - 1]) {
result.add(arr[i]);
}
}
return result;
}
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
ArrayList<Integer> result = removeDuplicates(arr);
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i) + " ");
}
}
}
from typing import List
def removeDuplicates(arr: List[int]) -> List[int]:
n = len(arr)
if n <= 1:
return arr
# Start from the second element
idx = 1
result = []
result.append(arr[0])
for i in range(1, n):
if arr[i]!= arr[i - 1]:
result.append(arr[i])
return result
if __name__ == "__main__":
arr = [1, 2, 2, 3, 4, 4, 4, 5, 5]
result = removeDuplicates(arr)
for i in range(len(result)):
print(result[i], end=' ')
using System;
using System.Collections.Generic;
public class MainClass {
public static List<int> removeDuplicates(int[] arr) {
int n = arr.Length;
List<int> result = new List<int>();
result.Add(arr[0]);
for (int i = 1; i < n; i++) {
if (arr[i]!= arr[i - 1]) {
result.Add(arr[i]);
}
}
return result;
}
public static void Main(string[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
List<int> result = removeDuplicates(arr);
for (int i = 0; i < result.Count; i++) {
Console.Write(result[i] + " ");
}
}
}
function removeDuplicates($arr) {
$n = count($arr);
if ($n <= 1)
return $arr;
// Start from the second element
$idx = 1;
$result = [];
array_push($result, $arr[0]);
for ($i = 1; $i < $n; $i++) {
if ($arr[$i]!= $arr[$i - 1]) {
array_push($result, $arr[$i]);
}
}
return $result;
}
$arr = [1, 2, 2, 3, 4, 4, 4, 5, 5];
$result = removeDuplicates($arr);
for ($i = 0; $i < count($result); $i++) {
echo $result[$i] . " ";
}
?>
Output
1 2 3 4 5