Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to define set data structure without using library set class in Python
A set is a collection of unique elements. We can implement a custom set data structure without using Python's built-in set class by creating a class with basic set operations.
Set Operations to Implement
Our custom set class will support the following methods ?
- Constructor to create a new set instance
-
add(val)to insert an integer into the set -
exists(val)to check if a value exists in the set -
remove(val)to remove a value from the set
Implementation Approach
We'll use a dictionary where each key represents a value and maps to a list containing that value. This approach ensures uniqueness while providing fast lookups ?
- Use
defaultdict(list)to automatically create empty lists for new keys - Check existence before adding to prevent duplicates
- Use dictionary operations for efficient insertion and removal
Example
from collections import defaultdict
class MySet:
def __init__(self):
self.buckets = defaultdict(list)
def add(self, val):
if not self.exists(val):
self.buckets[val].append(val)
def exists(self, val):
return val in self.buckets[val]
def remove(self, val):
if self.exists(val):
del self.buckets[val]
# Test the custom set implementation
s = MySet()
s.add(10)
s.add(20)
s.add(10) # Duplicate - won't be added
print("10 exists:", s.exists(10))
s.remove(10)
print("10 exists after removal:", s.exists(10))
print("20 exists:", s.exists(20))
10 exists: True 10 exists after removal: False 20 exists: True
How It Works
Let's trace through the operations ?
-
s.add(10)− Adds 10 to the set since it doesn't exist -
s.add(20)− Adds 20 to the set since it doesn't exist -
s.add(10)− Does nothing since 10 already exists -
s.exists(10)− Returns True because 10 is in the set -
s.remove(10)− Removes 10 from the set -
s.exists(10)− Returns False since 10 was removed -
s.exists(20)− Returns True because 20 is still in the set
Alternative Implementation Using a List
Here's a simpler approach using just a list to store unique values ?
class SimpleSet:
def __init__(self):
self.data = []
def add(self, val):
if val not in self.data:
self.data.append(val)
def exists(self, val):
return val in self.data
def remove(self, val):
if val in self.data:
self.data.remove(val)
# Test the simple set implementation
simple_set = SimpleSet()
simple_set.add(5)
simple_set.add(15)
simple_set.add(5) # Duplicate
print("Values in set:", simple_set.data)
print("5 exists:", simple_set.exists(5))
simple_set.remove(5)
print("Values after removing 5:", simple_set.data)
Values in set: [5, 15] 5 exists: True Values after removing 5: [15]
Conclusion
Both implementations successfully create a set data structure without using Python's built-in set class. The dictionary approach offers better performance for large datasets, while the list approach is simpler and more intuitive for smaller collections.
