Linear Probing in Python Last Updated : 17 May, 2024 Comments Improve Suggest changes Like Article Like Report Linear probing is a technique used in hash tables to handle collisions. When a collision occurs (i.e., when two keys hash to the same index), linear probing searches for the next available slot in the hash table by incrementing the index until an empty slot is found. What is Linear Probing?In linear probing, the hash table is searched sequentially that starts from the original location of the hash. If in case the location that we get is already occupied, then we check for the next location. Linear Probing Algorithm:Calculate the hash key. i.e. key = data % sizeCheck, if hashTable[key] is emptystore the value directly by hashTable[key] = dataIf the hash index already has some value then check for next index using key = (key+1) % sizeCheck, if the next index is available hashTable[key] then store the value. Otherwise try for next index.Do the above process till we find the space.Below is the implementation of the above approach: Python class LinearProbeHashTable: def __init__(self, size): self.size = size self.keys = [None] * size self.values = [None] * size def hash_function(self, key): return hash(key) % self.size def put(self, key, value): index = self.hash_function(key) # Linear probing to find an empty slot while self.keys[index] is not None: if self.keys[index] == key: # If key already exists, update its value self.values[index] = value return index = (index + 1) % self.size # Insert the key-value pair self.keys[index] = key self.values[index] = value def get(self, key): index = self.hash_function(key) # Linear probing to find the key while self.keys[index] is not None: if self.keys[index] == key: return self.values[index] index = (index + 1) % self.size # Key not found return None hash_table = LinearProbeHashTable(10) hash_table.put('apple', 5) hash_table.put('banana', 7) hash_table.put('orange', 3) print(hash_table.get('banana')) # Output: 7 print(hash_table.get('grape')) # Output: None Output7 None Time complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Linear Probing in Python N nikhilgarg527 Follow Improve Article Tags : Hash DSA Python-DSA Practice Tags : Hash Similar Reads Interpolation in Python Interpolation in Python refers to the process of estimating unknown values that fall between known values. This concept is commonly used in data analysis, mathematical modeling, and graphical representations. Python provides several ways to perform interpolation, including the use of libraries like 2 min read Matrix manipulation in Python In python matrix can be implemented as 2D list or 2D Array. Forming matrix from latter, gives the additional functionalities for performing various operations in matrix. These operations and array are defines in module "numpy". Operation on Matrix : 1. add() :- This function is used to perform eleme 4 min read Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each 3 min read Simple Linear Regression in Python Simple linear regression models the relationship between a dependent variable and a single independent variable. In this article, we will explore simple linear regression and it's implementation in Python using libraries such as NumPy, Pandas, and scikit-learn.Understanding Simple Linear RegressionS 7 min read Numeric Operations in Pandas Pandas is a powerful data manipulation and analysis library for Python. It provides versatile data structures like series and dataframes, making it easy to work with numeric values. In this article, we will explore five different methods for performing numeric value operations in Pandas, along with 4 min read Like