BackmediumArraysPhonePe

NEW OR EXISTING ID 4 Solution

Problem Statement

You are provided with a sequence of integers representing a data stream. Your task is to filter this sequence by eliminating every element that has the value 4. The resulting sequence must preserve the relative order of the remaining elements. If the input sequence contains no instances of the value 4, the output should be identical to the input. If all elements are 4, the output should be an empty sequence.

The function should accept a single array of integers as input and return a new array containing only the elements that are not equal to 4. You must not modify the original array in place; instead, construct and return a new array. This operation is a standard filtering task where the predicate for exclusion is a strict equality check against the integer 4.

Example 1
Input
nums = [1, 4, 2, 4, 3]
Output
[1, 2, 3]

Explanation: Iterate through the input array. The first element 1 is not 4, so it is kept. The second element 4 is excluded. The third element 2 is kept. The fourth element 4 is excluded. The fifth element 3 is kept. The final filtered array is [1, 2, 3].

Example 2
Input
nums = [4, 4, 4]
Output
[]

Explanation: Every element in the input array is 4. Therefore, all elements are excluded from the result. The output is an empty array.

Example 3
Input
nums = [10, 20, 30]
Output
[10, 20, 30]

Explanation: The input array does not contain the value 4. Since no elements meet the exclusion criteria, the output array is identical to the input array.

Example 4
Input
nums = [4, 1, 4, 2, 4, 3, 4]
Output
[1, 2, 3]

Explanation: The array is scanned from left to right. Elements at indices 0, 2, 4, and 6 are 4 and are removed. Elements at indices 1, 3, and 5 are 1, 2, and 3 respectively, and are retained in their original relative order. The result is [1, 2, 3].

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The input array is guaranteed to be non-empty.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

NEW OR EXISTING ID 4 — Problem Statement & Solution Guide

ArraysMedium
TimeO(N)
|
SpaceO(1) (in‑place) or O(N) (new array)

Problem Description

You are provided with a sequence of integers representing a data stream. Your task is to filter this sequence by eliminating every element that has the value 4. The resulting sequence must preserve the relative order of the remaining elements. If the input sequence contains no instances of the value 4, the output should be identical to the input. If all elements are 4, the output should be an empty sequence.

The function should accept a single array of integers as input and return a new array containing only the elements that are not equal to 4. You must not modify the original array in place; instead, construct and return a new array. This operation is a standard filtering task where the predicate for exclusion is a strict equality check against the integer 4.

DSA Pattern Breakdown

DSA Pattern Breakdown

"NEW OR EXISTING ID 4"

medium

WHY DOES IT MATTER?

Filtering is a foundational pattern for data cleaning, validation, and transformation; mastering it prevents hidden bugs when downstream logic assumes the absence of certain values.

OPTIMIZATION CHALLENGE

The key insight is to avoid repeated deletions that shift elements; instead, either collect qualifying items in a new buffer or overwrite unwanted slots using a write pointer, turning a quadratic operation into linear.

REAL-WORLD CONNECTION

Think of a firewall that drops packets matching a blacklist rule; the remaining traffic must keep its original sequence to avoid protocol violations, mirroring the array‑filter problem.

During an interview, start by stating the O(N) single‑pass solution, then discuss in‑place vs. extra‑space variants, and mention edge‑case handling (empty array, all elements filtered) to demonstrate thoroughness.

COMPLEXITY AT A GLANCE

⏱ Time:O(N)
💾 Space:O(1) (in‑place) or O(N) (new array)

Core Theory — Why This Approach?

The problem is a classic example of linear filtering, where we need to traverse a sequence and retain only elements that satisfy a predicate (value != 4). A naive solution might repeatedly search for the value 4 and splice the array, which incurs O(n^2) time due to repeated shifting of elements. The optimal paradigm leverages a single pass, either building a new list of qualifying elements or using two‑pointer in‑place overwriting, guaranteeing O(n) time because each element is examined exactly once. This approach aligns with the "stream processing" model: treat the input as an immutable data stream and produce a filtered output while preserving order, which is essential for correctness in most real‑world pipelines.

Interview Questions on This Problem

Q1How would you remove all occurrences of a specific value from an array in-place without using extra space?

Use a two‑pointer technique: maintain a write index that only advances when a non‑target element is encountered; for each read index, if arr[read] != target, assign arr[write] = arr[read] and increment write. After the loop, truncate the array to the write length.

Q2What is the time and space complexity of filtering a stream of N integers to exclude a particular value, and why can't we do better than O(N) time?

The optimal solution runs in O(N) time because each element must be inspected at least once to decide whether to keep it. The space can be O(1) if we modify the array in place, or O(N) if we construct a new array for the result.

Q3In a distributed log‑processing system, how would you design a component that drops all events with a certain flag (e.g., error code 4) while preserving event order?

Implement a stateless filter microservice that reads events from the input stream, checks the flag, forwards only events where flag != 4 to the downstream topic, and relies on the underlying message broker to maintain order for the filtered subset.

Examples

Example 1

Input

nums = [1, 4, 2, 4, 3]

Output

[1, 2, 3]

Explanation: Iterate through the input array. The first element 1 is not 4, so it is kept. The second element 4 is excluded. The third element 2 is kept. The fourth element 4 is excluded. The fifth element 3 is kept. The final filtered array is [1, 2, 3].

Example 2

Input

nums = [4, 4, 4]

Output

[]

Explanation: Every element in the input array is 4. Therefore, all elements are excluded from the result. The output is an empty array.

Example 3

Input

nums = [10, 20, 30]

Output

[10, 20, 30]

Explanation: The input array does not contain the value 4. Since no elements meet the exclusion criteria, the output array is identical to the input array.

Example 4

Input

nums = [4, 1, 4, 2, 4, 3, 4]

Output

[1, 2, 3]

Explanation: The array is scanned from left to right. Elements at indices 0, 2, 4, and 6 are 4 and are removed. Elements at indices 1, 3, and 5 are 1, 2, and 3 respectively, and are retained in their original relative order. The result is [1, 2, 3].

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The input array is guaranteed to be non-empty.

Optimal Approach & Strategy

Perform a single pass, appending non‑4 elements to a result list or overwriting in place with a write index, achieving linear time.

Brute Force Approach

Repeatedly search for the value 4 and delete it, causing the array to shift each time, which leads to quadratic time.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function filterOutFour(nums) {
    return nums.filter(x => x !== 4);
}

Asked in Top Tech Interviews

PhonePe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.