{"id":22349071,"url":"https://github.com/afsify/datastructure","last_synced_at":"2026-02-26T17:04:22.791Z","repository":{"id":217347644,"uuid":"743161570","full_name":"afsify/datastructure","owner":"afsify","description":"Level up your JavaScript skills with a progression of data structure exercises, starting from basics and advancing to more complex concepts. Clear code examples provided for easy understanding.","archived":false,"fork":false,"pushed_at":"2024-10-21T13:23:52.000Z","size":127,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-14T12:45:26.674Z","etag":null,"topics":["data-structures","javascript","workouts"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/afsify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-01-14T14:20:02.000Z","updated_at":"2024-10-21T13:23:56.000Z","dependencies_parsed_at":"2024-01-22T15:30:13.544Z","dependency_job_id":"0edb5158-fa8f-4a6f-b454-63772de01fd4","html_url":"https://github.com/afsify/datastructure","commit_stats":null,"previous_names":["mhdafs/datastructure","afsify/datastructure"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/afsify/datastructure","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afsify%2Fdatastructure","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afsify%2Fdatastructure/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afsify%2Fdatastructure/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afsify%2Fdatastructure/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/afsify","download_url":"https://codeload.github.com/afsify/datastructure/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afsify%2Fdatastructure/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29865428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T16:38:37.846Z","status":"ssl_error","status_checked_at":"2026-02-26T16:37:58.932Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-structures","javascript","workouts"],"created_at":"2024-12-04T11:07:16.613Z","updated_at":"2026-02-26T17:04:22.745Z","avatar_url":"https://github.com/afsify.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data Structures in JavaScript\n\nData structures are ways of organizing, managing, and storing data in a computer program. They facilitate efficient data manipulation and retrieval, making them essential for developing efficient algorithms. JavaScript provides several built-in data structures, and understanding them is key to writing optimized code.\n\n## Core Data Structures\n\n### 1. **Array**\n- **Description**: An ordered collection of elements identified by index or key. Arrays can store multiple values in a single variable.\n- **Usage**: Useful for storing lists of items, such as numbers, strings, or objects.\n- **Example**:\n  ```javascript\n  const fruits = ['apple', 'banana', 'cherry'];\n  ```\n\n#### Operations\n- **Insertion**: `array.push(value)` to add an element to the end.\n- **Deletion**: `array.pop()` to remove the last element.\n- **Traversal**: Use loops (e.g., `for`, `forEach`) to access each element.\n\n### 2. **Object**\n- **Description**: Unordered collections of key-value pairs. Keys are strings (or Symbols), and values can be of any data type.\n- **Usage**: Ideal for representing structured data like user profiles or settings.\n- **Example**:\n  ```javascript\n  const person = {\n      name: 'John',\n      age: 30,\n      isStudent: false\n  };\n  ```\n\n#### Operations\n- **Access**: `person.name` or `person['name']`.\n- **Modification**: `person.age = 31;`.\n\n### 3. **Set**\n- **Description**: A collection of unique values. A Set allows you to store values without duplicates.\n- **Usage**: Useful for tasks that require uniqueness, such as tracking distinct items.\n- **Example**:\n  ```javascript\n  const uniqueNumbers = new Set([1, 2, 3, 2, 1]); // {1, 2, 3}\n  ```\n\n#### Operations\n- **Add**: `set.add(value)`.\n- **Delete**: `set.delete(value)`.\n- **Check existence**: `set.has(value)`.\n\n### 4. **Map**\n- **Description**: A collection of key-value pairs where keys can be of any data type, and insertion order is maintained.\n- **Usage**: Suitable for mapping data, where order and uniqueness of keys matter.\n- **Example**:\n  ```javascript\n  const map = new Map();\n  map.set('name', 'Alice');\n  map.set('age', 25);\n  ```\n\n#### Operations\n- **Access**: `map.get(key)`.\n- **Delete**: `map.delete(key)`.\n\n### 5. **Linked List**\n- **Description**: A linear collection of elements (nodes), where each node points to the next node. It allows for dynamic memory allocation.\n- **Usage**: Efficient for insertions and deletions at any position.\n- **Example**: Requires a custom implementation.\n\n#### Operations\n- **Insertion**: Add nodes at the beginning, end, or middle.\n- **Deletion**: Remove nodes by updating references.\n\n### 6. **Stack**\n- **Description**: A collection that follows the Last In, First Out (LIFO) principle. The last added element is the first to be removed.\n- **Usage**: Useful for managing function calls (call stack) or undo mechanisms.\n- **Example**:\n  ```javascript\n  class Stack {\n      constructor() {\n          this.items = [];\n      }\n      push(element) {\n          this.items.push(element);\n      }\n      pop() {\n          return this.items.pop();\n      }\n  }\n  ```\n\n### 7. **Queue**\n- **Description**: A collection that follows the First In, First Out (FIFO) principle. The first added element is the first to be removed.\n- **Usage**: Useful for scheduling tasks or managing requests.\n- **Example**:\n  ```javascript\n  class Queue {\n      constructor() {\n          this.items = [];\n      }\n      enqueue(element) {\n          this.items.push(element);\n      }\n      dequeue() {\n          return this.items.shift();\n      }\n  }\n  ```\n\n### 8. **Tree**\n- **Description**: A hierarchical data structure consisting of nodes, with a root node and subtrees. Each node can have multiple children.\n- **Usage**: Useful for representing hierarchical data, such as file systems or organizational structures.\n- **Example**: Binary trees, where each node has at most two children.\n\n### 9. **Graph**\n- **Description**: A collection of nodes (vertices) connected by edges. Graphs can be directed or undirected.\n- **Usage**: Useful for representing networks, such as social networks or routing paths.\n- **Example**: Implementing a graph typically requires an adjacency list or matrix.\n\n### 10. **HashTable**\n- **Description**: A data structure that maps keys to values for efficient lookup, using a hash function.\n- **Usage**: Useful for fast data retrieval and management.\n- **Example**: JavaScript's `Object` can act as a hash table.\n\n### 11. **Heap**\n- **Description**: A specialized tree-based data structure where the parent node is greater (or smaller) than its children, commonly used in priority queues.\n- **Usage**: Useful for algorithms that require frequent access to the maximum or minimum element.\n- **Example**: Binary heap implementations for priority queues.\n\n### 12. **Trie**\n- **Description**: A tree-like data structure where keys are usually strings, often used for storing and searching strings efficiently.\n- **Usage**: Useful in applications like autocomplete and spell-checking.\n- **Example**: Requires a custom implementation with nodes representing characters.\n\n### 13. **String**\n- **Description**: A sequence of characters used to represent text data.\n- **Usage**: Commonly used for manipulation, searching, and comparison in applications.\n\n## Search Algorithms\n### Common Search Algorithms\n- **Linear Search**: Iterates through each element in the array to find a target value.\n- **Binary Search**: Efficiently finds a target value in a sorted array by repeatedly dividing the search interval in half.\n\n### Example of Binary Search:\n```javascript\nfunction binarySearch(arr, target) {\n    let left = 0;\n    let right = arr.length - 1;\n\n    while (left \u003c= right) {\n        const mid = Math.floor((left + right) / 2);\n        if (arr[mid] === target) {\n            return mid;\n        }\n        if (arr[mid] \u003c target) {\n            left = mid + 1;\n        } else {\n            right = mid - 1;\n        }\n    }\n    return -1; // Not found\n}\n```\n\n## Sort Algorithms\n### Common Sorting Algorithms\n- **Bubble Sort**: Simple sorting algorithm that repeatedly steps through the list, comparing adjacent elements.\n- **Merge Sort**: A divide-and-conquer algorithm that divides the list into halves and merges them in a sorted manner.\n- **Quick Sort**: A divide-and-conquer algorithm that selects a pivot element and partitions the array around it.\n\n### Example of Quick Sort:\n```javascript\nfunction quickSort(arr) {\n    if (arr.length \u003c= 1) return arr;\n\n    const pivot = arr[arr.length - 1];\n    const left = [];\n    const right = [];\n\n    for (let i = 0; i \u003c arr.length - 1; i++) {\n        if (arr[i] \u003c pivot) {\n            left.push(arr[i]);\n        } else {\n            right.push(arr[i]);\n        }\n    }\n    return [...quickSort(left), pivot, ...quickSort(right)];\n}\n```\n\n## Best Practices for Using Data Structures\n\n### Choosing the Right Data Structure\n- **Understand Requirements**: Analyze the types of operations required (insertions, deletions, searches) and choose a structure that optimizes those operations.\n- **Consider Space Complexity**: Assess the memory requirements of each structure.\n- **Evaluate Time Complexity**: Understand the time complexity of operations like insertion, deletion, and retrieval.\n\n### Memory Management\n- **Avoid Memory Leaks**: Ensure that references are removed when no longer needed, especially in linked structures.\n- **Use Built-in Structures When Possible**: JavaScript's built-in arrays, objects, sets, and maps are optimized for performance.\n\n### Code Organization\n- **Modular Design**: Implement data structures in a modular fashion for easier maintenance and testing.\n- **Documentation**: Comment on the purpose of each structure and its intended use to improve code readability.\n\n## Common JavaScript Commands for Data Structures\n\n- **Array Operations**:\n  ```javascript\n  const arr = [1, 2, 3, 4];\n  arr.push(5); // Add to the end\n  arr.pop();   // Remove from the end\n  ```\n\n- **Object Operations**:\n  ```javascript\n  const obj = { key1: 'value1', key2: 'value2' };\n  obj.key3 = 'value3'; // Add new key-value pair\n  delete obj.key1;     // Remove a key\n  ```\n\n- **Set Operations**:\n  ```javascript\n  const mySet = new Set();\n  mySet.add(1);\n  mySet.add(2);\n  mySet.has(1); // true\n  ```\n\n- **Map Operations**:\n  ```javascript\n  const myMap = new Map();\n  myMap.set('a', 1);\n  myMap.get('a'); // 1\n  ```\n\n## Clone the Repository\n\nIn the terminal, use the following command:\n\n```bash\ngit clone https://github.com/mhdafs/datastructure.git\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafsify%2Fdatastructure","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fafsify%2Fdatastructure","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafsify%2Fdatastructure/lists"}