MASTER YOUR LOGIC BUILDING:Phase 4 - Level 2: Searching & Counting Logic

Mastering phase 4 - level 2: searching & counting logic concepts and implementation.

Phase 4 - Level 2: Searching & Counting Logic

Why Search and Count?

Once you have a list of data, the next natural questions are: "Is my target here?", "Where is it?", and "How many times does it show up?" You won't use fancy algorithms yet — just walk through the list one item at a time. That's linear search, and it's the foundation for everything harder later.

Imagine checking lockers in a hallway until you find the right number — you don't skip ahead; you look at each one.

What You'll Learn

  • Linear search — find an element's index (or know it's missing)
  • Count occurrences — how many times a value appears
  • When to stop early (break) vs scan the whole list

Linear Search — Walk Until You Find It

index = -1          # -1 means "not found yet"
for i in range(len(arr)):
    if arr[i] == target:
        index = i
        break       # stop — we found the first match

Step-by-step thinking:

  1. Pick a "not found" sentinel: -1 or a found = False flag
  2. Compare each arr[i] to target
  3. On match, save the index and break
  4. After the loop, check if you found anything
First vs last occurrence: To find the first, break on match. To find the last, don't break — keep updating the index every time you see a match.

Count Occurrences — Visit Every Match

count = 0
for element in arr:
    if element == target:
        count += 1    # no break — count ALL matches

Search stops at the first hit; counting must see every element.

Step-by-Step Thinking Process

  1. What am I looking for? — a value, a condition (even numbers, values > 10)?
  2. What do I track? — index, count, or a yes/no flag?
  3. Stop early or go to the end? — search usually breaks; counting never breaks early.
  4. Empty list? — loop doesn't run; count stays 0, index stays -1.

Common Mistakes & Tips

  • Forgetting `break` in search — you might overwrite the index with a later duplicate.
  • Using `break` when counting — you'll under-count.
  • Checking `if arr[i]` inside a `for element in arr` loop — pick one style; index-based when you need the position.
  • Tip: Say the goal first: "I need every match" → no break. "I need the first match" → break.

Practice Focus

Run Linear Search in Array and Count Occurrences below with duplicate values in your test data — that makes the break vs no-break difference obvious. Try a target that isn't in the list and confirm your "not found" handling works.

Hands-on Examples

Linear Search in Array

# Take array and target
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Enter element {i+1}: ")))

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

# Linear search
found = False
index = -1
for i in range(len(arr)):
    if arr[i] == target:
        found = True
        index = i
        break

if found:
    print(f"Element found at index {index}")
else:
    print("Element not found")

Iterate through array, compare each element with target. If found, store index and break. If loop completes, element not found.