/27. Remove Element

27. Remove Element

Easy
Arrays61.5% acceptance

Given an integer array input_array and an integer target_value, remove all occurrences of target_value from input_array in-place. The order of elements may be changed. Return the number of elements remaining in input_array that are not equal to target_value. After removal, the first k elements of input_array must contain the elements not equal to target_value. The remaining elements are not important.

Example 1

Input: input_array = [5, 1, 5, 7], target_value = 5

Output: 2

Explanation: After removal, input_array could be [1, 7, _, _], and the function returns 2.

Example 2

Input: input_array = [10, 20, 10, 30, 40], target_value = 10

Output: 3

Explanation: After removal, input_array could be [20, 30, 40, _, _], and the function returns 3.

Constraints

  • 0 <= len(input_array) <= 100
  • 0 <= input_array[i] <= 50 for all valid i
  • 0 <= target_value <= 100
Python (current runtime)

Case 1

Input: input_array = [8, 8, 2, 3], target_value = 8

Expected: 2

Case 2

Input: input_array = [0, 0, 0, 0], target_value = 0

Expected: 0

Case 3

Input: input_array = [12, 15, 18], target_value = 20

Expected: 3

Case 4

Input: input_array = [], target_value = 1

Expected: 0

Case 5

Input: input_array = [7, 6, 5, 4, 3], target_value = 4

Expected: 4