Find Common Elements Practice Problem
This data science coding problem helps you practice Data Structures, find common elements, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Data Structures.
- Problem ID: 85
- Problem key: 85-find-common-elements
- URL: https://datacrack.app/solve/85-find-common-elements
- Difficulty: easy
- Topic: Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Find Common Elements
---
### 🎯 Goal
Finding elements that appear across multiple collections is the **set intersection** operation. In data science, this comes up when finding common features across datasets, common users across cohorts, or common categories across time periods.
---
### 🔍 Set Intersection
The intersection of multiple lists is the set of elements that appear in **all** of them:
```
[1, 2, 3, 4] ∩ [2, 3, 5, 6] ∩ [3, 2, 7, 8] = {2, 3}
```
---
### 💻 Task
Implement `find_common(lists)` using set operations.
---
### 📥 Input
- `lists`: a list of lists — two or more sub-lists
### 📤 Output
- A **sorted** list of elements that appear in every sub-list
---
### 🧩 Starter Code
```python
def find_common(lists):
"""
Find elements that appear in every list.
Args:
lists (list[list]): A list of lists
Returns:
list: Sorted list of common elements
"""
if not lists:
return []
# 🧠 TODO: Convert the first list to a set
# 🧠 TODO: For each remaining list, intersect with the current common set
# 🧠 TODO: Return the result as a sorted list
pass
```
---
### 💡 Example
```python
find_common([[1, 2, 3, 4], [2, 3, 5, 6], [3, 2, 7, 8]])
# Expected: [2, 3]
find_common([[1, 2, 3], [4, 5, 6]])
# Expected: []
```
---
### 🔑 Key Concepts
- `set(list)` — convert list to set (removes duplicates, enables set operations)
- `set_a & set_b` or `set_a.intersection(set_b)` — set intersection
- `sorted(set)` — convert back to sorted list
- Start with the first list's elements and progressively narrow downStarter Code
def find_common(lists):
"""
Find elements that appear in every list.
Args:
lists (list[list]): A list of lists
Returns:
list: Sorted list of common elements
"""
if not lists:
return []
# 🧠 TODO: Convert the first list to a set
# 🧠 TODO: For each remaining list, intersect with the current common set
# 🧠 TODO: Return the result as a sorted list
pass