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 find: "))
# Find first occurrence
if x in arr:
index = arr.index(x)
print(f"First occurrence at index: {index}")
else:
print("Element not found")Output
Enter array size: 5 Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 20 Element 5: 40 Enter element to find: 20 First occurrence at index: 1
Use index() method to find first occurrence.
Key Concepts:
- index(x) returns first index
- Check existence first
- Returns index of first match