/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 the remaining elements may be changed. Return the number of elements in input_array that are not equal to target_value. After removal, the first k elements of input_array should contain the elements not equal to target_value. The contents beyond the first k elements are not important.

Example 1

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

Output: 2

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

Example 2

Input: input_array = [7, 8, 9, 7, 10], target_value = 7

Output: 3

Explanation: After removal, input_array could be [8, 9, 10, _, _], 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 = [4, 4, 4, 4], target_value = 4

Expected: 0

Case 2

Input: input_array = [12, 13, 14, 15], target_value = 16

Expected: 4

Case 3

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

Expected: 2

Case 4

Input: input_array = [], target_value = 1

Expected: 0

Case 5

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

Expected: 0