-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove_Element.java
More file actions
31 lines (31 loc) · 970 Bytes
/
Remove_Element.java
File metadata and controls
31 lines (31 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution
{
public int removeElement(int[] nums, int val)
{
int start=0; //start index.
int end = nums.length-1; //last index.
int count=0;
while(start<=end)
{
if(nums[end]==val) //checking if the last element matches with the val..
{
count++; //increase the count..
end--; //and decrease the end index..
}
else if(nums[start]==val) //if start index matches with the val..
{
count++;
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
else //if neither of them matches..then simply increase start index..
{
start++;
}
}
return nums.length-count; //return how many numbers did not match with the val..
}
}