🧠
Brute Force (Nested Loops)
💡 This approach exists to build intuition by explicitly checking each element against all others, showing why naive methods are inefficient.
Intuition
For each element, scan the rest of the array to check if it is a duplicate and remove it by shifting elements left.
Algorithm
- Start from the first element and compare it with every other element to the right.
- If a duplicate is found, shift all subsequent elements one position to the left.
- Reduce the array length by one after each removal.
- Repeat until all duplicates are removed.
💡 This algorithm is straightforward but inefficient because it repeatedly shifts elements, which is costly.
def remove_duplicates(nums):
n = len(nums)
i = 0
while i < n:
j = i + 1
while j < n:
if nums[j] == nums[i]:
for k in range(j, n - 1):
nums[k] = nums[k + 1]
n -= 1
else:
j += 1
i += 1
return n
# Driver code
if __name__ == '__main__':
arr = [0,0,1,1,1,2,2,3,3,4]
length = remove_duplicates(arr)
print(length) # Expected: 5
print(arr[:length]) # Expected: [0,1,2,3,4]
Line Notes
n = len(nums)Store original length to track array size as we remove duplicates
while i < n:Outer loop picks each element to compare against others
if nums[j] == nums[i]:Check if current element is a duplicate
for k in range(j, n - 1):Shift elements left to overwrite duplicate
n -= 1Reduce effective array size after removal
else:
j += 1Only advance j if no removal to avoid skipping elements
return nReturn new length after all duplicates removed
public class Solution {
public static int removeDuplicates(int[] nums) {
int n = nums.length;
int i = 0;
while (i < n) {
int j = i + 1;
while (j < n) {
if (nums[j] == nums[i]) {
for (int k = j; k < n - 1; k++) {
nums[k] = nums[k + 1];
}
n--;
} else {
j++;
}
}
i++;
}
return n;
}
public static void main(String[] args) {
int[] arr = {0,0,1,1,1,2,2,3,3,4};
int length = removeDuplicates(arr);
System.out.println(length); // Expected: 5
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Line Notes
int n = nums.length;Keep track of current array length as duplicates are removed
while (i < n) {Outer loop selects each element for comparison
if (nums[j] == nums[i]) {Detect duplicate element
for (int k = j; k < n - 1; k++) {Shift elements left to overwrite duplicate
n--;Decrease array size after removal
else { j++; }Advance j only if no removal to avoid skipping elements
return n;Return new length after processing
#include <iostream>
#include <vector>
using namespace std;
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
int i = 0;
while (i < n) {
int j = i + 1;
while (j < n) {
if (nums[j] == nums[i]) {
for (int k = j; k < n - 1; k++) {
nums[k] = nums[k + 1];
}
n--;
} else {
j++;
}
}
i++;
}
return n;
}
int main() {
vector<int> arr = {0,0,1,1,1,2,2,3,3,4};
int length = removeDuplicates(arr);
cout << length << endl; // Expected: 5
for (int i = 0; i < length; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Line Notes
int n = nums.size();Track current size of array as duplicates are removed
while (i < n) {Outer loop picks each element to compare
if (nums[j] == nums[i]) {Check for duplicate element
for (int k = j; k < n - 1; k++) {Shift elements left to overwrite duplicate
n--;Reduce array size after removal
else { j++; }Advance j only if no removal to avoid skipping elements
return n;Return new length after all duplicates removed
function removeDuplicates(nums) {
let n = nums.length;
let i = 0;
while (i < n) {
let j = i + 1;
while (j < n) {
if (nums[j] === nums[i]) {
for (let k = j; k < n - 1; k++) {
nums[k] = nums[k + 1];
}
n--;
} else {
j++;
}
}
i++;
}
return n;
}
// Driver code
const arr = [0,0,1,1,1,2,2,3,3,4];
const length = removeDuplicates(arr);
console.log(length); // Expected: 5
console.log(arr.slice(0, length)); // Expected: [0,1,2,3,4]
Line Notes
let n = nums.length;Store current array length to track removals
while (i < n) {Outer loop selects each element for duplicate check
if (nums[j] === nums[i]) {Detect duplicate element
for (let k = j; k < n - 1; k++) {Shift elements left to overwrite duplicate
n--;Decrease array length after removal
else { j++; }Advance j only if no removal to avoid skipping elements
return n;Return new length after duplicates removed
For each element, we scan the rest of the array and shift elements on duplicates, leading to quadratic time.
💡 For n=20, this means roughly 400 operations, which is inefficient for large arrays.
Interview Verdict: TLE / Inefficient for large inputs
This approach is too slow for large inputs but helps understand the problem and why optimization is needed.