-
Notifications
You must be signed in to change notification settings - Fork 0
202051024 28 remove element #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e719c9c
aa78379
84a2ed2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function removeElement(nums: number[], val: number): number { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const removedNum = -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let i = nums.length - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let j = nums.length - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (i >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nums[i] === val) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (i === j) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nums[j] = removedNum; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nums[i] = nums[j]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nums[j] = removedNum; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| j--; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| i--; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return j + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2
to
+20
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const removedNum = -1; | |
| let i = nums.length - 1; | |
| let j = nums.length - 1; | |
| while (i >= 0) { | |
| if (nums[i] === val) { | |
| if (i === j) { | |
| nums[j] = removedNum; | |
| } else { | |
| nums[i] = nums[j]; | |
| nums[j] = removedNum; | |
| } | |
| j--; | |
| } | |
| i--; | |
| } | |
| return j + 1; | |
| }; | |
| let k = 0; | |
| for (let i = 0; i < nums.length; i++) { | |
| if (nums[i] !== val) { | |
| nums[k] = nums[i]; | |
| k++; | |
| } | |
| } | |
| return k; | |
| } |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Remove the semicolon after the closing brace. Function declarations don't require trailing semicolons in TypeScript/JavaScript.
| }; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conditional check
i === jis unnecessary. When i equals j,nums[i] = nums[j]is effectively a no-op assignment of the same value, and the assignmentnums[j] = removedNumcan be done unconditionally. This logic can be simplified to remove the conditional branch.