Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check if Element Exists

Check whether an entered integer occurs in a list. Students use membership for a yes-or-no search and compare existence with finding a position or frequency.

IntermediatePhase 4 - Level 2: Searching & Counting LogicExample 1 of 10
check-if-element-exists.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7x = int(input("Enter element to search: "))
8
9# Check existence
10if x in arr:
11 print(f"{x} exists in array")
12else:
13 print(f"{x} does not exist in array")

Output

Enter array size: 5
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 20
Element 5: 40
Enter element to search: 20
20 exists in array

In this one

  • Ask a membership question
  • Search unsorted data
  • Separate existence from frequency

How it works

  1. 1

    Collect arr and read x.

  2. 2

    Evaluate x in arr.

  3. 3

    Print one existence or absence message.

Edge cases

What happens for an empty list?

What happens for an empty list?

The not-exists message is printed.

Does a duplicate produce extra messages?

Does a duplicate produce extra messages?

No. The membership decision is made once.

What's going on

x in arr tests membership after the values have been collected. It answers whether any element equals x.

The program prints only an existence message, not an index or count. Multiple occurrences still produce one positive message. Membership does not require sorted input and does not change the list; an empty list simply contains no candidate.

What happens for an empty list? The not-exists message is printed.

Does a duplicate produce extra messages? No. The membership decision is made once.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does in return conceptually?

Pick an answer to check it.

Question 2

What happens for an empty list?

Pick an answer to check it.

Questions people ask

What happens for an empty list?

The not-exists message is printed.

Does a duplicate produce extra messages?

No. The membership decision is made once.

What does in return conceptually?

A boolean answer.