Skip to content

Commit cf2af19

Browse files
committed
Add remove element algorithm
1 parent 87ab876 commit cf2af19

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ List of Programs related to data structures and algorithms
5555
| 14 | First missing positive number | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.md) | Hard | In-place hashing |
5656
| 15 | Best time to buy stock and sell stock | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.md) | Easy | Greedy algorithm |
5757
| 16 | Two missing numbers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.md) | Medium | Sum and average calculations |
58+
| 17 | Pascal's Triangle | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.md) | Easy | Array traversal and sequential sum |
59+
| 18 | Remove Element | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.md) | Easy | Array traversal |
5860

5961
### String
6062

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package java1.algorithms.array.removeElement;
2+
3+
public class RemoveElement {
4+
5+
private static int removeElement(int[] nums, int val){
6+
int k = 0;
7+
8+
for (int num : nums) {
9+
if (num != val) {
10+
nums[k] = num;
11+
k++;
12+
}
13+
}
14+
15+
return k;
16+
}
17+
public static void main(String[] args) {
18+
int[] nums1 = new int[]{4, 1, 1, 4, 1};
19+
int val1=4;
20+
System.out.println(removeElement(nums1, val1));
21+
int[] nums2 = new int[]{0, 3, 5, 5, 3, 1, 5, 2};
22+
int val2=5;
23+
System.out.println(removeElement(nums2, val2));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
**Description:**
2+
Given an array of integers `nums` and an integer `val`, remove all occurrences of `val` from `nums` in-place and return the number of elements which are not equals to `val`.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [4, 1, 1, 4, 1], val=4
8+
Output: 3
9+
10+
Example 2:
11+
12+
Input: nums = [0, 3, 5, 5, 3, 1, 5, 2], val=5
13+
Output: 5
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of iteration over an array. The algorithmic approach can be summarized as follows:
17+
18+
1. Initialize the index variable(`k`) for position the elements of an array(`nums`) which are not equal to given value(i.e, `val`).
19+
20+
2. Iterate over the array and compare each value with `val`.
21+
22+
3. If the current value is not equal to `val`, store the current value in kth position and increment the index variable(i.e, `k++`).
23+
24+
4. After completion of iteration, the variable `k` indicates the number of elements which are not equal to given number.
25+
26+
**Time and Space complexity:**
27+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once.
28+
29+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than index variables.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function removeElement(nums, val){
2+
let k=0;
3+
for (const num of nums) {
4+
if(num !== val) {
5+
nums[k] = num;
6+
k++;
7+
}
8+
}
9+
10+
return k;
11+
}
12+
13+
const nums1 = [4, 1, 1, 4, 1], val1=4;
14+
console.log(removeElement(nums1, val1));
15+
const nums2 = [0, 3, 5, 5, 3, 1, 5, 2], val2=5;
16+
console.log(removeElement(nums2, val2));
17+
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
**Description:**
2+
Given an array of integers `nums` and an integer `val`, remove all occurrences of `val` from `nums` in-place and return the number of elements which are not equals to `val`.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [4, 1, 1, 4, 1], val=4
8+
Output: 3
9+
10+
Example 2:
11+
12+
Input: nums = [0, 3, 5, 5, 3, 1, 5, 2], val=5
13+
Output: 5
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of iteration over an array. The algorithmic approach can be summarized as follows:
17+
18+
1. Initialize the index variable(`k`) for position the elements of an array(`nums`) which are not equal to given value(i.e, `val`).
19+
20+
2. Iterate over the array and compare each value with `val`.
21+
22+
3. If the current value is not equal to `val`, store the current value in kth position and increment the index variable(i.e, `k++`).
23+
24+
4. After completion of iteration, the variable `k` indicates the number of elements which are not equal to given number.
25+
26+
**Time and Space complexity:**
27+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once.
28+
29+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than index variables.

0 commit comments

Comments
 (0)