Skip to content

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions removeelement/index.test.js
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", () => {

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?

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.

Copy link
Member Author

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.

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);
});
});
16 changes: 16 additions & 0 deletions removeelement/question.txt
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.
19 changes: 19 additions & 0 deletions removeelement/solution.js
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]

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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;