Check if Element Exists

Check if x exists in array.

Logic BuildingIntermediate
Logic Building
# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Element {i+1}: ")))

x = int(input("Enter element to search: "))

# Check existence
if x in arr:
    print(f"{x} exists in array")
else:
    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

Use 'in' operator to check membership.

Key Concepts:

  • 'in' operator checks existence
  • Returns True if found
  • Simple membership test