-
Notifications
You must be signed in to change notification settings - Fork 3
Remove a val in arr and return the length of remaining values #10
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
Open
ezkemboi
wants to merge
1
commit into
master
Choose a base branch
from
removeelement
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+54
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { expect } = require("chai") | ||
const removeElement = require("./solution"); | ||
|
||
describe("remove element", () => { | ||
it("should remove an element and return its length", () => { | ||
const result = removeElement([3, 2, 1, 3], 3); | ||
expect(result).to.equal(2); | ||
}); | ||
|
||
it("returns 4 when val is not in an arr below", () => { | ||
const result = removeElement([3, 2, 1, 3], 5); | ||
expect(result).to.equal(4); | ||
}); | ||
|
||
it("should return 0 when arr is empty", () => { | ||
const result = removeElement([], 3); | ||
expect(result).to.equal(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-> Given an array arr and a value val, | ||
-> remove all instances of that value in-place | ||
-> and return the new length. | ||
|
||
-> Do not allocate extra space for another array, | ||
-> you must do this by modifying the input array in-place with O(1) extra memory. | ||
|
||
-> The order of elements can be changed. | ||
-> It doesn't matter what you leave beyond the new length. | ||
|
||
Example:- | ||
|
||
-> Given arr = [3,2,2,3], val = 3, | ||
-> Your function should return length = 2 | ||
-> with the first two elements of arr being 2. | ||
-> It doesn't matter what you leave beyond the returned length. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const removeElement = (arr, val) => { | ||
// initialize count to zero | ||
let counts = 0; | ||
// loop through the arra | ||
for (let i = 0; i < arr.length; i++) { | ||
/** | ||
* check if num is not equal to val | ||
* then increment the number of counts | ||
* but assigining its num values | ||
*/ | ||
if (arr[i] != val) { | ||
arr[counts++] = arr[i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is not correct, take an example: arr = [1, 2, 4, 2, 5];
val = 2;
// when i = 0, arr[i] != val is true, so
// arr[counts++] = arr[i], basically arr[0] = arr[0], nothing has happened here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will check on this. |
||
} | ||
} | ||
// return our counts | ||
return counts; | ||
} | ||
|
||
exports = module.exports = removeElement; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should also add a test to show that the array is mutated; coz how about if I just count the elements and return that count?
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 length should be different/less.
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.
Alright, feedback taken and will work on it.