Intuition:
The problem asks us to remove all occurrences of a given value
val
from the array nums
in-place and return the count of elements in the modified array. The order of the elements may change after the removal.Approach:
- Counting Occurrences: We first need to determine how many occurrences of
val
exist in the arraynums
. This can be done by iterating through the array and counting each occurrence.
- Two Pointers Approach: After counting occurrences, we calculate the desired length of the modified array by subtracting the count of
val
occurrences from the total length of the array. Then, we use a two-pointers approach to modify the array in-place. - Initialize two pointers,
i
at the beginning (index 0) andj
at the end (indexn - 1
, wheren
is the length of the array). - Iterate until
i
crosses the desired length or meetsj
. - If
nums[i]
is equal toval
andnums[j]
is not equal toval
, swapnums[i]
andnums[j]
, decrementj
, and incrementi
. - If
nums[i]
is not equal toval
, incrementi
. - If
nums[j]
is equal toval
, decrementj
. - Repeat until
i
crosses the desired length or meetsj
.
- Return Length of Modified Array: Finally, return the desired length of the modified array.
Complexity Analysis:
- Time Complexity:
- Counting occurrences: O(n), where n is the length of the array
nums
. - Two pointers approach: O(n), where n is the length of the array
nums
. - Overall, the time complexity is O(n).
- Space Complexity:
- The solution modifies the array in-place without using any additional space. Hence, the space complexity is O(1).
Code
public class Solution { public int RemoveElement(int[] nums, int val) { int n = nums.Length; int k = 0; foreach(int a in nums) { if(a == val) k++; } int i = 0; int j = n-1; int ret = n - k; while(i < ret && j > 0 && i < n) { if(nums[i] == val && nums[j] != val) { nums[i] = nums[j]; j--; i++; } else if(nums[i] != val) i++; else if(nums[j] == val) j--; } return ret; } }
Summary:
The solution efficiently removes all occurrences of a given value
val
from the array nums
in-place using a two-pointers approach. It maintains the order of the elements and returns the count of non-val
elements in the modified array. The time complexity of the solution is O(n), where n is the length of the array nums
, and the space complexity is O(1).Blog Posts
Went on business trip to China
A Note on a business trip to China. Meeting several Chinese biggest refrigerator, freezer manufacturing companies
Jan 18, 2025
Business
Trip
Startups
Career
Projects
Thought Experiments
Tech
Creating a Barcode Detector Using the Browser BarcodeDetector API
In this tutorial, we'll create a simple barcode detector web application using the Browser BarcodeDetector API. The app will access the user's webcam, display the video stream, and detect barcodes in real-time.
Jun 17, 2024
Javascript
Computer Science
Video
Web Dev
Software Development
App Dev
Thought Experiments
Tech
Coding
Programming
26. Remove Duplicates from Sorted Array
Solving leetcode problem 26. Remove duplicates from sorted array using two pointer approach.
Apr 8, 2024
C#
Leetcode
Coding
Competitive programming
Problem solving
Building your portfolio website backed by Notion as CMS
Publish your portfolio notion page to the web under 30 minutes using Next.js.
Apr 2, 2024
Node.js
Next.js
Problem solving
Blog
Lifestyle
Programming
Web Dev
45. Jump Game II
Solve leetcode 45. Jump Game II using greedy approach.
Apr 15, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
Greedy Algorithm
122. Best Time to Buy and Sell Stock II
Solve leetcode 122. Best Time to Buy and Sell Stock II using greedy algorithm
Apr 10, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
121. Best Time to Buy and Sell Stock
Solve leetcode 121. Best Time to Buy and Sell Stock using simple greedy algorithm.
Apr 9, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
169. Majority Element
Solve leetcode 169. Majority Element with sorting method.
Apr 9, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
80. Remove Duplicates from Sorted Array II
Intuition and algorithm to solve 80. Remove Duplicates from Sorted Array II
Apr 3, 2024
Leetcode
Coding
Problem solving
C#
1768. Merge strings Alternately (Beats 96.66%)
2 pointer approach for solving a problem that merges two strings alternately.
Mar 29, 2024
C#
Leetcode
Computer Science
Coding
Programming
Problem solving
Empowering legacy KonvaJS in React projects
Building visual editor tools with Konva and React.
Coding
Problem solving
Creativity
Library
Node.js
React.js
Konva.js