Arrays, Strings And Data Handling: Searching Through Arrays Step By Step (Copy)
Searching Through Arrays Step By Step
What “Searching Through Arrays” Means
- Searching means checking array elements to:
- Find a specific value (target)
- Find whether the value exists
- Find the position/index of a value
- Find all occurrences
- In AS Level style questions, searching is usually:
- Linear search (sequential)
- Key skills assessed:
- Correct traversal
- Correct use of conditions
- Safe bounds
- Correct outputs (found/not found, index)
Before You Start: Search Requirements Checklist
- What must be output?
- Found / Not found
- Index position
- First occurrence only
- All positions
- Count of occurrences
- What is the data type?
- INTEGER, REAL, STRING, CHAR, BOOLEAN
- Does the array contain duplicates?
- If yes, define whether you need first or all matches
Key Search Variables (Used In Almost All Answers)
| Variable | Type | Purpose |
|---|---|---|
target |
same as array type | value to find |
index |
INTEGER | current position being checked |
found |
BOOLEAN | tracks whether target has been located |
pos |
INTEGER | stores found position (if required) |
count |
INTEGER | counts matches |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Step-By-Step Linear Search (Find First Occurrence)
Goal
- Search
arr[1..n]fortarget - Output:
- “Found” and the index
- OR “Not found”
Step 1: Initialise Control Variables
- Start from first element
- Assume not found
index ← 1
found ← FALSE
Step 2: Loop With Safe Bounds + Early Exit
- Loop must stop when:
- index exceeds n
- OR found becomes TRUE
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
Step 3: Output Based On Found
IF found = TRUE THEN
OUTPUT "Found at ", index
ELSE
OUTPUT "Not found"
ENDIF
Full Copy-Paste Version (Exam-Safe)
index ← 1
found ← FALSE
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
IF found = TRUE THEN
OUTPUT "Found at ", index
ELSE
OUTPUT "Not found"
ENDIF
Dry-Run Example (How To Trace Searching)
Given
arr[1..6] = [12, 7, 19, 7, 30, 5]target = 7
Trace Table
| Step | index | arr[index] | Compare to target | found |
|---|---|---|---|---|
| Start | 1 | 12 | 12 = 7? No | FALSE |
| 1 | 2 | 7 | 7 = 7? Yes | TRUE |
- Loop stops early
- Output:
Found at 2
Common Search Variations (Step-By-Step)
Variation A: Find All Occurrences
- Goal
- Output all indices where target occurs
- Key difference
- Do not stop at first match
FOR index ← 1 TO n
IF arr[index] = target THEN
OUTPUT "Found at ", index
ENDIF
NEXT index
- Pitfall
- Using WHILE + found flag stops too early
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Variation B: Count Occurrences
- Goal
- Count how many times target occurs
count ← 0
FOR index ← 1 TO n
IF arr[index] = target THEN
count ← count + 1
ENDIF
NEXT index
OUTPUT count
- Examiner focus
- Counter initialised
- Counter updated only when matched
Variation C: Return Position Or -1 (Common Pattern)
- Goal
- If found, output index
- Else output -1
pos ← -1
FOR index ← 1 TO n
IF arr[index] = target THEN
pos ← index
EXIT FOR
ENDIF
NEXT index
OUTPUT pos
- If
EXIT FORis not allowed in your expected pseudocode style:- Use a
foundflag instead:
- Use a
pos ← -1
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
pos ← index
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
OUTPUT pos
Variation D: Search For Minimum / Maximum By Traversal
- Not “searching for target” but still a search-type question
- Maximum:
max ← arr[1]
posMax ← 1
FOR index ← 2 TO n
IF arr[index] > max THEN
max ← arr[index]
posMax ← index
ENDIF
NEXT index
OUTPUT max
OUTPUT posMax
Searching In Two-Dimensional Arrays (Table Search)
Goal
- Find target in
table[1..r, 1..c] - Stop when found
- Output row and column
Step-By-Step Safe Approach (Flag + Nested Loops)
found ← FALSE
FOR row ← 1 TO r
FOR col ← 1 TO c
IF table[row, col] = target THEN
found ← TRUE
foundRow ← row
foundCol ← col
ENDIF
NEXT col
NEXT row
IF found = TRUE THEN
OUTPUT "Found at ", foundRow, ",", foundCol
ELSE
OUTPUT "Not found"
ENDIF
- Note
- This finds the last occurrence if duplicates exist
- To find the first occurrence, add protection:
found ← FALSE
FOR row ← 1 TO r
FOR col ← 1 TO c
IF table[row, col] = target AND found = FALSE THEN
found ← TRUE
foundRow ← row
foundCol ← col
ENDIF
NEXT col
NEXT row
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Common Logic Errors In Search Questions (High-Frequency)
Error 1: Out Of Range Access
- Example
- Loop continues after
index = n
- Loop continues after
- Fix
- Always use
index <= nin condition
- Always use
Error 2: Index Not Incremented
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ENDIF
ENDWHILE
- Infinite loop if first element isn’t target
- Fix
- Increment index when not found
Error 3: Using Wrong Variable In Comparison
IF arr[i] = target THEN
- But loop variable is
index - Fix
- Keep loop variable consistent
Error 4: Forgetting To Initialise found
foundleft undefined- Fix
- Always set
found ← FALSE
- Always set
Error 5: Searching Strings Incorrectly
- Comparing parts incorrectly
- Fix
- Use exact equality unless question asks partial match
Dry-Run Checklist For Search Questions
- Write array values with indices
- Start at index 1
- For each step:
- Compare
arr[index]withtarget - If match, stop (for first-match search)
- Else increment index
- Compare
- Confirm stop condition reached:
- Found OR index becomes
n + 1
- Found OR index becomes
Exam-Ready “Search Toolkit” Table
| Required Output | Best Pattern |
|---|---|
| Found / Not found | WHILE + flag |
| First position | WHILE + flag (store pos) |
| All positions | FOR (no early exit) |
| Count occurrences | FOR + counter |
| Row/col in 2D | Nested FOR + flag |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
One-Line Rules To Memorise
- Linear search checks each element in order
- Always protect bounds (
index <= n) - Use
foundflag for early exit - Use FOR when you need all matches
- Update index every cycle unless found
