### Radix Sort Algorithm

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

August 26, 2020

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Note:

- We have to return the new length
- And, modify the array also

**Constraint**

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

```
# Example 1
Given nums = [1,1,2],
Output = 2
# Example 2
Given nums = [0,0,1,1,1,2,2,3,3,4],
Output = 5
```

First think out loud about the problem.

- Its a sorted array,
- And, which means a number which is repeating would occur later after another number.

Lets look at the code

```
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int num = nums[0];
int j=1;
for (int i=1; i<nums.length; i++) {
if (num != nums[i]) {
num = nums[i];
nums[j] = nums[i];
j ++;
}
}
return j;
}
```

**Steps**

- We can begin with first index value, save it in a variable.
- We can keep another variable for storing our index
`j`

, which will point to index upto which our array is unique. - This index variable will increment after finding a unique value only.
- And, we need to copy that unique value to our array as well.

```
Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 41.1 MB, less than 87.80% of Java online submissions for Remove Duplicates from Sorted Array.
```

```
public int removeDuplicates2(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int j=1;
for (int i=1; i<nums.length; i++) {
if (nums[j-1]!= nums[i]) {
nums[j]= nums[i];
j ++;
}
}
return j;
}
```

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

Problem Statement Given a string, find the length of the longest substring…

Counting sort runs on relatively smaller set of input. Counting sort calculates…

First try to understand question. Its a binary tree, not a binary search tree…

Problem Statement Given a Binary tree, print out nodes in level order traversal…

Max Priority Queue is a data structure which manage a list of keys(values). And…

Introduction Strapi is a backend system provides basic crud operations with…

Introduction I had to create many repositories in an Github organization. I…

Introduction I was trying to download some youtube videos for my kids. As I have…

Introduction In this post, we will explore some useful command line options for…

Introduction In this post, we will see how we can apply a patch to Python and…

Introduction We will introduce a Package Manager for Windows: . In automations…