Data Structure & Algorithms
  • 🖌️Unlocking the Power of Algorithms with C#
  • Data Structure
    • Data Structure
    • Big O
    • Array
    • Linked Lists
    • Stacks
    • Queues
    • Hash Tables
    • Trees
    • Graphs
    • Heap Sort
    • ParkingLot Algorithm
    • LRU cache
    • Priority Queue
  • Algorithms
    • Algorithm
    • Recursion
    • Sorting
    • Searching
    • Dynamic Programming
  • Problems
    • Array
      • Two Sum
      • Two Sum II - Input Array Is Sorted
      • Contains Duplicate
      • Maximum Subarray
      • House Robber
      • Move Zeroes
      • Rotate Array
      • Plus One
      • Find number of subarrays with even length
      • Find number of subarrays with even sum
      • Find Missing Element
      • Reduce Array Size to The Half
      • Remove Duplicates
      • Merge Sorted Arrays
      • Arrays Intersection
      • 3Sum
      • Trapping Rain Water
      • Maximum sum of a subarray
      • Longest Subarray
      • Subarray Sum Equals K
      • Container With Most Water
      • Missing Number
      • Roman to Integer
      • First Missing Positive
      • Unique Integers That Sum Up To 0
      • Integer to Roman
      • Flatten
    • String
      • Check if two strings are permutation of each other
      • String Compression
      • Palindrome Permutation
      • Determine if a string has all Unique Characters
      • One Away
      • Longest Substring Without Repeating Characters
      • Valid Palindrome
      • Valid Palindrome II
      • Backspace String Compare
      • First Unique Character in a String
      • Is Subsequence
      • URLify a given string
      • String has same characters at same position
      • Number of Ways to Split a String
      • Check whether two Strings are anagram of each other
      • Print last `n` lines of a big file or big string.
      • Multiply Strings
    • Matrix
      • Search a 2D Matrix
      • Search a 2D Matrix II
      • Rotate Matrix
      • Spiral Matrix
      • Set Matrix Zeroes
    • Bit Manipulation
      • Sum of Two Integers
      • Reverse Number
      • Reverse Number II
      • Binary Bits Count
      • Binary Bits Count II
    • Stack
      • Valid Parentheses
      • Balance or not expression
      • Decode String
    • Tree
      • Binary Tree Inorder Traversal
      • Binary Tree Preorder Traversal
      • Binary Tree Postorder Traversal
      • Binary Tree Level Order Traversal
      • Binary Tree Return All Root-To-Leaf Paths
      • Binary Tree Height-Balanced
      • Valid Binary Search Tree
      • Binary Tree Sum of all left leaves.
    • Linked List
      • Linked List Delete Middle Node
      • Merge Sorted Linked List
      • Reverse Linked List
      • Remove Duplicates from Sorted List
      • Remove Duplicates from Unsorted List
      • Linked List Cycle
    • Graph
      • The Number Of Islands
      • Number of Closed Islands
      • Max Area of Island
      • Rotting Oranges
      • Number of Provinces
      • Course Schedule
      • Surrounded Regions
      • Snakes and Ladders
      • Widest Path Without Trees
      • Knight Probability in Chessboard
      • Possible moves of knight
      • Check Knight Tour Configuration
      • Steps by Knight
      • Network Delay Time
    • Greedy
      • Best Time to Buy and Sell Stock
      • Best Time to Buy and Sell Stock II
      • Smallest Subset Array
      • Jump Game
    • Backtracking
      • Towers of Hanoi
      • Subsets
      • Combination Sum
      • Sudoku Solver
      • Word Search
    • Heap
      • Kth Largest Element in an Array
      • Top K Frequent Elements
    • Sorting
      • Order Colors String
    • Recursion
      • Number To Text
      • Divide Number
Powered by GitBook
On this page
  • Example
  • Operations of the Hash Table in data structure
  • Hash Collision
  1. Data Structure

Hash Tables

A Hash Table is a data structure used to insert, look up, and remove key-value pairs quickly. It operates on the concept of hashing, where each key is translated by a hash function into a distinct index in an array. The index functions as a storage location for the matching value. In simple words, it maps the keys with the values. It provides direct access to its items in constant time and ensures that for each key, data occurs at most once. Dictionaries and maps in programming languages which is generally used in caching systems for faster data retrieval and database indexing for quick lookups.

Example

// Initialize a regular dictionary
var dict = new Dictionary<string, int>()
            {
                {"apple", 1},
                {"banana", 2},
                {"cherry", 3}
            };

Operations of the Hash Table in data structure

There are some operations of the Hash Table in the data structure:

  • Access: Accessing an element in a hash table is done using the key. The key is processed through the hash function to generate the index where the corresponding value can be found.

  • Insertion: This operation involves inserting a new key-value pair into the hash table. The key is processed through a hash function to generate an index where the corresponding value is stored.

  • Deletion: This operation involves removing a key-value pair from the hash table. Similar to the search operation, the key is processed through the hash function to find the index where the corresponding value is stored, and then the key-value pair is removed from that index.

  • Search: This operation involves searching for a key-value pair in the hash table. The key is processed through the same hash function to generate the index where the corresponding value can be found.

  • Update: Updating a value in a hash table is similar to the access operation. The key is used to find the corresponding value in the hash table. Once the value is found, it can be updated with the new value.

  • Sort: N/A.

Complexity Table

Operation
Best Case
Average Case
Worst Case

Insertion

O(1)

O(1)

O(N)

Search

O(1)

O(1)

O(N)

Deletion

O(1)

O(1)

O(N)

Update

O(1)

O(1)

O(N)

Hash Collision

In computer science, a hash collision occurs when two distinct pieces of data in a hash table share the same hash value. This happens when a hash function, which takes a data input and returns a fixed length of bits, maps different data to the same hash. There are strategies to handle them, like chaining (linked lists) or open addressing.

PreviousQueuesNextTrees

Last updated 1 year ago