BackmediumArrays PhonePe

NEW OR EXISTING ID Solution

Problem Statement

Given an array of integers and a new id, return the array with the new id if it already exists in the array, otherwise return the array with the new id appended.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 7
Output
[1, 2, 3, 4, 5, 6, 7]

Explanation: Step-by-step: Given the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and the new id 7, we check if 7 exists in the array. Since it does, we return the array with 7. No additional id is appended.

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

Explanation: Step-by-step: Given the array [1, 2, 3, 4, 5, 6] and the new id 4, we check if 4 exists in the array. Since it does, we return the array with 4. No additional id is appended.

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