DEV Community

Abubakar Sadiq Ismail
Abubakar Sadiq Ismail

Posted on

Day 2 I4G #10Dayscodechallenge

Todays challenge was removing an Element from an array, with O(1) space complexity.
E.g given arr= [1,2,3,4,3,3,3] remove all 3 in arr
return the number of array without the element which is 3.
Question Tag: Easy

The first thing that came to my mind is to use pointer from index 0;
pointer = 0;
Iterate through the array;
If current iteration value is not the same as the element
update pointer with it;
Increment pointer

return the value of pointer;
with O(n) time complexity and O(1) space complexity;

Pseudocode
array = [...], val = x;
Pointer = 0
for item in array
if item != val
array[pointer] = item
pointer++

return pointer

Code Implementation

Leet code problem

Top comments (0)