PHP – Remove Duplicates from Sorted Array | LeetCode Problem
Hello learners,
In today’s tutorial, we will learn how to Remove Duplicates from Sorted Array in PHP
Let’s see the code first then you see a step by step guide of the explanation of the below code.
Here,
$nums = [0,0,1,1,1,2,2,3,3,4];
This is our array from where we will remove the duplicates. By the way, this is an sorted array.
class Solution { public function removeDuplicates(&$nums) { $n = count($nums); if ($n == 0) return 0; // If the array is empty, there are no duplicates $uniqueCount = 1; // At least one unique element is present at the start of the array for ($i = 1; $i < $n; $i++) { // Check if the current element is different from the previous one if ($nums[$i] != $nums[$i - 1]) { // If it's different, it's a new unique element // Copy the new unique element to the next available position in the array // Increment the count of unique elements $nums[$uniqueCount] = $nums[$i]; $uniqueCount++; } } // Return the count of unique elements return $uniqueCount; } } // Example usage: $nums = [0,0,1,1,1,2,2,3,3,4]; $solution = new Solution(); $k = $solution->removeDuplicates($nums); echo "After Removing Duplicates: " . implode(", ", array_slice($nums, 0, $k)) . "\n"; echo "Number of Unique Elements: $k\n";
Now if we analyze the code, we can see that:
- We initialize
$uniqueCount
to 1 because at least one unique element is present. - We iterate through the array starting from the second element.
- If the current element is different from the previous one, it means it’s a new unique element, so we copy it to the next available position in the array.
- Finally, we return the value of
$uniqueCount
, which represents the number of unique elements in the modified array.
This solution modifies the array in-place and returns the number of unique elements, keeping the relative order of elements intact.
Time Complexity of the given solution:
The time complexity (Big O) of the provided code is O(n), where n is the number of elements in the input array $nums
. Here’s why:
- The code iterates through the input array once, from the second element (
$i = 1
) to the last element ($i = $n - 1
), where$n
is the number of elements in the array. - During each iteration, the code performs constant-time operations such as comparisons and array element assignments (
$nums[$uniqueCount] = $nums[$i]
). - As each element of the array is processed once, the overall time complexity is linear with respect to the size of the input array, resulting in O(n) time complexity.
Therefore, the time complexity of the code is O(n), indicating that the time required to execute the code grows linearly with the size of the input array.
Check Out More Resources:
Articles:
Website: https://laravelplug.com/
YouTube Channel: https://www.youtube.com/channel/UCnTxncHQcBSNs4RCuhWgF-A?sub_confirmation=1
WordPress Playlist: https://www.youtube.com/watch?v=8VvXzFAFwQU&list=PLVoVZqAh8dTLHZ51egvEU7dsOrRCSIMWx
Tools Playlist: https://www.youtube.com/watch?v=gTQoUn3BQkM&list=PLVoVZqAh8dTK-DWHBGWOEEvzvPQrgFne-
WordPress Tutorials: https://laravelplug.com/category/wordpress/
Laravel Tutorials: https://laravelplug.com/category/laravel/
PHP Tutorials: https://laravelplug.com/category/php/
SQL Tutorials: https://laravelplug.com/category/sql/
Various Tips: https://laravelplug.com/category/tips/
Useful Tools: https://laravelplug.com/category/tools/
Socials:
Twitter: https://twitter.com/LaravelPlug
Pinterest: https://www.pinterest.com/LaravelPlugCom/
Facebook: https://www.facebook.com/groups/1020759861842360
Mail: info@laravelplug.com
#PHP #array #leetcode
That’s All. Feel free to knock me. Thanks.