- RECURSION
- Call Stack
- Iterative Approach
- Memoization
- Head Tail
- Mental Checklist
- Dfs Traversal
- Bfs Traversal
- DATA STRUCTURES
- Lists
-
Sets
- Time Complexity
- Hash Tables
- Stacks
- Queues
- Linked Lists
- Trees
- Binary Search Trees
- Tries
- Graphs
- COMPARISION BASED
- Bubble Sort
- Selection Sort
- Merge Sort
- DIVIDE CONQUER
- Binary Search
- Quicksort
- Karatsuba Multiplication
- GRAPH TRAVERSAL
- Flood Fill
- Depth First Search
- Breadth First Search
- Dijkstra's Algorithm
- DECISION MAKING
- Minimax
- GREEDY
- Coin Change
- Fractional Knapsack
ALGORITHMS PAGES - LEVEL 3
Inserting sets - O(1)
""" INSERTING SETS - O(1)
-------------------------
In terms of time compexity, the only difference
between arrays and sets is at insertion:
- Reading O(1)
- Searching O(N)
- Inserting O(1)
- Deleting O(N)
Concept:
A set stores UNIQUE, UNORDERD elements. It rejects duplicates.
Insertion is (on average) O(1) thanks to hashing.
Analogy:
Think of a guest list where each name can appear only once.
Adding a new name is instant; trying to add an existing name changes nothing.
Time complexity:
Average: O(1) to check + insert
Notes:
- Order is NOT guaranteed, SET chooses the storage location.
- Duplicates are ignored (no change to the set).
- This function MUTATES the input set.
---------------------------------------
"""
fruits = {'apples', 'bananas', 'oranges'}
def add_item(items, value):
steps = 0
# Duplicate check
for item in items:
steps += 1
if item == value:
return -1
# Not found -> insert
items.add(value)
steps += 1
return steps
steps = add_item(fruits, 'kiwi')
print(fruits, steps)
# ------------------------------------------
# {'bananas', 'apples', 'kiwi', 'oranges'} 4