General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Data structures

Map/dictionary

Maps, often referred to as dictionaries in languages like Python, are data structures that store data in key-value pairs. Each element in a map consists of a unique key and a value associated with that key. Maps allow for efficient retrieval, insertion, and deletion of values based on their keys, making them highly useful for situations where data access is primarily done through known identifiers.

Key Characteristics:

  • Key-Value Pairing: Each value stored in the map is associated with a unique key. The key is used to access or modify the corresponding value.

  • Efficiency: Most map implementations provide very efficient lookup, insertion, and deletion operations, often with time complexity close to O(1), depending on the implementation and the complexity of the hash function (in the case of hash maps).

  • Unordered and Ordered Maps: Some programming languages distinguish between unordered maps (where elements are not stored in any particular order) and ordered maps (where elements are stored in a sorted order or the order of insertion). For example, Python 3.7 and later maintain insertion order for dictionaries, effectively making them ordered.

Common Operations:

  • Insertion: Add a new key-value pair to the map.

  • Deletion: Remove a key-value pair from the map using the key.

  • Lookup: Retrieve the value associated with a specific key.

  • Update: Change the value associated with a given key.

  • Check Existence: Verify whether a key is present in the map.

Uses:

Maps are incredibly versatile and used in a wide range of programming scenarios, including:

  • Implementing associative arrays, where an array can be indexed not just with integers, but with keys of any data type.

  • Counting the occurrences of elements in a collection (e.g., counting words in a document).

  • Storing the relationships between objects (e.g., the parent-child relationship in a tree structure).

  • Caching data by storing previously computed results keyed by their input parameters for quick retrieval.

Implementation:

Under the hood, maps can be implemented using various data structures, including hash tables (a common implementation for unordered maps) and binary search trees (for ordered maps). The choice of implementation affects the performance characteristics of the map, especially in terms of time complexity for different operations.

Below is a simple example in Python that demonstrates how to use a dictionary (Python's implementation of a map) to count the occurrences of each word in a given sentence. Python dictionaries are versatile and straightforward to use, making them an excellent illustration of map functionality.

# Define a sentence
sentence = "this is a sample sentence this sentence is just a sample"

# Split the sentence into words
words = sentence.split()

# Create an empty dictionary to store word counts
word_counts = {}

# Iterate over each word in the list of words
for word in words:
    # If the word is already in the dictionary, increment its count
    if word in word_counts:
        word_counts[word] += 1
    # If the word is not in the dictionary, add it with a count of 1
    else:
        word_counts[word] = 1

# Print the word counts
for word, count in word_counts.items():
    print(f"'{word}': {count}")

In this example, we first split the sentence into individual words. Then, we iterate over these words, using the dictionary word_counts to keep track of how many times each word appears. If a word is encountered for the first time, it is added to the dictionary with a count of 1. If it is encountered again, we simply increment its count. Finally, we print out the count of each word.

This code illustrates key map operations, including insertion, update, and retrieval, showcasing how maps can be used to efficiently solve common programming problems.

PreviousStacksNextTrees

Last updated 1 year ago

Was this helpful?