BackmediumArrays Flipkart

NEW OR EXISTING ID 3 Solution

Problem Statement

Given an array of integers and an integer ID, return the array with the ID either added if it's not present or removed if it's already present.

Example 1
Input
[1, 2, 3, 5, 6] and ID = 4
Output
[1, 2, 3, 4, 5, 6]

Explanation: Step 1: Check if ID 4 is present in the array. Since it's not present, we proceed to Step 2. Step 2: Add ID 4 to the array. The updated array is [1, 2, 3, 4, 5, 6].

Example 2
Input
[1, 2, 3, 4, 5, 6] and ID = 4
Output
[1, 2, 3, 5, 6]

Explanation: Step 1: Check if ID 4 is present in the array. Since it's present, we proceed to Step 2. Step 2: Remove ID 4 from the array. The updated array is [1, 2, 3, 5, 6].

Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free