Skip to content

Commit 41c5b7e

Browse files
authored
Add functions for removing duplicates and counting items
1 parent 8cfff6e commit 41c5b7e

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def remove_duplicates(items):
2+
"""Remove duplicates while preserving the first occurrence order."""
3+
result = []
4+
for item in items:
5+
if item not in result:
6+
result.append(item)
7+
return result
8+
9+
10+
def list_counts(items):
11+
"""Return the frequency of each item."""
12+
counts = {}
13+
for item in items:
14+
counts[item] = counts.get(item, 0) + 1
15+
return counts
16+
17+
18+
def reverse_dict(mapping):
19+
"""Swap keys and values; the last key wins for repeated values."""
20+
return {value: key for key, value in mapping.items()}

0 commit comments

Comments
 (0)