MASTER YOUR LOGIC BUILDING:Phase 4 - Level 4: Aggregation & Comparison
Mastering phase 4 - level 4: aggregation & comparison concepts and implementation.
Phase 4 - Level 4: Aggregation & Comparison
Why This Matters
Until now you've mostly worked with one list. Real problems often involve two lists: Are these two attendance sheets the same? Which students appear in both classes? Can we combine two sorted scoreboards into one?
This level teaches you to compare, merge, and find overlaps between arrays — skills you'll use in data cleaning, search, and interviews.
What You'll Learn
- How to check if two arrays are identical (same order)
- How to find common elements (intersection)
- How to merge / concatenate arrays
- What to check first when lengths differ
Always Ask First: Same Length?
Many comparison problems fail immediately if lengths differ:
if len(arr1) != len(arr2):
print("Different")
else:
# now compare element by element
same = True
for i in range(len(arr1)):
if arr1[i] != arr2[i]:
same = False
break
print("Same" if same else "Different")
Step-by-step: Check sizes → walk indexes together → stop early when you find a mismatch.
Finding Common Elements
Think of two friend groups. Who is in both?
common = []
for element in arr1:
if element in arr2 and element not in common:
common.append(element)
element in arr2→ is it in the second list?element not in common→ avoid duplicates in the result
Merging Arrays
Simplest merge (concatenate):
merged = arr1 + arr2
# [1, 2] + [3, 4] → [1, 2, 3, 4]
If both lists are already sorted and you need a sorted result, walk both with two indexes (merge like in merge-sort) instead of just sticking them together and sorting again — but + is fine when order doesn't matter or you'll sort later.
Element-wise Ideas
When lists are the same length, you can pair positions:
sums = []
for i in range(len(arr1)):
sums.append(arr1[i] + arr2[i])
Tips
- Decide early: do you care about order or only about membership?
- Use a result list and
appendcarefully to avoid duplicates - For "is every item of A inside B?" (subset), loop A and check
in B
Common Mistakes
- Comparing without checking lengths first
- Forgetting to skip duplicates when building
common - Assuming
arr1 + arr2keeps sort order when inputs weren't sorted the way you expect
Practice Focus
Work through the examples: compare two arrays, find common elements, and merge. Say the plan before coding: "Length check → loop → collect result."
Hands-on Examples
Compare Arrays
# Take two arrays
n1 = int(input("Enter size of first array: "))
arr1 = []
for i in range(n1):
arr1.append(int(input(f"Array1 element {i+1}: ")))
n2 = int(input("Enter size of second array: "))
arr2 = []
for i in range(n2):
arr2.append(int(input(f"Array2 element {i+1}: ")))
# Compare arrays
if len(arr1) != len(arr2):
print("Arrays are not equal (different sizes)")
else:
equal = True
for i in range(len(arr1)):
if arr1[i] != arr2[i]:
equal = False
break
if equal:
print("Arrays are equal")
else:
print("Arrays are not equal")First check if lengths match. Then compare element by element. If any mismatch, arrays are not equal.
Related Page
🔗Related Content
- 💻
Phase 4 - Practice Problems
Practice Phase 4 concepts with hands-on coding problems
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with assessment questions
- ➡️
Phase 5 - Get Started
Continue to Phase 5 after mastering Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum