Learn
Exam

Removing Specific Elements from an Array

Difficulty
STARTER

Problem

Given an array numbers consisting of integer elements and an array deleted indicating whether to remove each element, write a function solution that removes elements from numbers according to deleted and returns the resulting array.

  • If deleted[i] is 1, numbers[i] is removed; if 0, numbers[i] is kept.

Constraints

  • Each element in numbers is an integer with an absolute value not exceeding 500.
  • numbers is not empty, and its length does not exceed 100.
  • Each element in deleted is either 1 or 0.
  • The length of deleted is equal to the length of numbers.

Test Case

Test Case Number numbers deleted return
1 [1, 2, 3, 4] [1, 0, 1, 0] [2, 4]
2 [1, 2, 3, 4] [0, 1, 0, 1] [1, 3]
3 [-490, 0, 35, -1, 500, -24] [1, 0, 0, 1, 1, 0] [0, 35, -24]

Explanation for Test Case 1

  • Given numbers = [1, 2, 3, 4] and deleted = [1, 0, 1, 0], elements 1 and 3 are removed, resulting in [2, 4].

Explanation for Test Case 2

  • Given numbers = [1, 2, 3, 4] and deleted = [0, 1, 0, 1], elements 2 and 4 are removed, resulting in [1, 3].

Explanation for Test Case 3

  • Given numbers = [-490, 0, 35, -1, 500, -24] and deleted = [1, 0, 0, 1, 1, 0], elements -490, -1, and 500 are removed, resulting in [0, 35, -24].