Why use an Array to implement a "list" instead of a Hash Table?
Last Updated :
20 Feb, 2024
What is an Array?
An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number.
Array
C++
#include <iostream>
using namespace std;
// driver program
int main() {
// initaizling array
int arr[] = {1, 2, 3, 4, 5};
// printing array element
for (int i : arr) {
cout << i << " ";
}
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
int arr[] = {1,2,3,4,5};
for(int i:arr){
System.out.print(i+" ");
}
}
}
Python3
# initializing list
arr = [1, 2, 3, 4, 5]
# printing list elements
for i in arr:
print(i, end=" ")
C#
using System;
class GFG {
public static void Main (string[] args) {
int[] arr = {1,2,3,4,5};
foreach(int i in arr){
Console.Write(i + " ");
}
}
}
// This code is contributed by akashish__
JavaScript
// Initializing array
const arr = [1, 2, 3, 4, 5];
// Printing array elements
for (const i of arr) {
console.log(i + " ");
}
What is a Hash Table
A Hash table is a data structure that stores some information, and the information has basically two main components, i.e., key and value. The hash table can be implemented with the help of an associative array.
What is a List?
A list is an ordered and changeable collection of data objects. Unlike an array, which can contain objects of a single type, a list can contain a mixture of objects.
C++
#include <iostream>
#include <vector>
int main() {
std::vector<int> ll = {1, 2, 3, 4, 5};
for (int num : ll) {
std::cout << num << " ";
}
return 0;
}
Java
/*package whatever //do not write package name here */
import java.util.*;
class GFG {
public static void main (String[] args) {
List<Integer> ll = Arrays.asList(new Integer[]{1,2,3,4,5});
System.out.println(ll);
}
}
Python3
# Python code with comments
# Define a list named 'll' containing integers 1 through 5
ll = [1, 2, 3, 4, 5]
# Iterate over each element 'num' in the list 'll'
for num in ll:
# Print the current element followed by a space
print(num, end=" ")
#this code is contributed by Utkarsh
C#
using System;
using System.Collections.Generic;
class GFG {
static void Main(string[] args) {
// Creating a list of integers using List<T> and initializing it with the provided array of integers
List<int> ll = new List<int>(new int[] { 1, 2, 3, 4, 5 });
// Printing the elements of the list
// Joining the elements into a single string separated by commas using string.Join
Console.WriteLine(string.Join(", ", ll));
}
}
JavaScript
// Main function
function main() {
// Define an array
let ll = [1, 2, 3, 4, 5];
// Iterate through the array and print each element
for (let num of ll) {
console.log(num + " ");
}
}
// Invoke main function
main();
Why use an "Array" to implement a "List" instead of a "Hash Table"?
All of the different collection data types have their specific advantages and disadvantages.
Time taken for insertion, lookup, and removal are O(1) for both arrays and hash tables in all cases. But array has a much lower constant overhead than hashtables. And arrays need less space per entry.Â
If you would like to remove and insert entries in a way that the following entries change their index accordingly, it would be O(n) in case of an array with n being the number of entries that need to be moved. It would also be O(n) for hashtables to do this but with a much higher constant overhead.
A list is typically ordered whereas a hashtable is not. So when you add elements into a list and expect the ordering to remain consistent then an array retains the ordering while a hashtable gives no guarantee as to the order you get back out. That's why we use an array to implement a list instead of a hash table.
Similar Reads
When to use Array over Linked List and vice versa? Array: An array is a linear data structure that is a collection of homogeneous elements. Arrays are stored in contiguous memory locations. An array is a static data structure that combines data of similar types. Example of an Array Linked List: A linked list is a linear data structure that contains
2 min read
When to use Array over a List? Array: Array is a linear data structure that is the collection of similar data types.  Arrays are static data structures with fixed sizes.  Arrays are stored in the contiguous memory allocation. An example of an ArrayLists in python: In a simple language, a list is a collection of things. Lists are
2 min read
Implementation of Hash Table in Python using Separate Chaining A hash table is a data structure that allows for quick insertion, deletion, and retrieval of data. It works by using a hash function to map a key to an index in an array. In this article, we will implement a hash table in Python using separate chaining to handle collisions. Components of hashing Sep
7 min read
What is the difference between lists and arrays? In programming, lists and arrays are data structures used to organize and store data. Both have their unique features and purposes. Lists are dynamic and flexible, allowing for easy resizing during runtime, while arrays are static with a fixed size. This difference impacts memory usage and performan
8 min read
Comparison of an Array and Hash table in terms of Storage structure and Access time complexity Arrays and Hash Tables are two of the most widely used data structures in computer science, both serving as efficient solutions for storing and accessing data in Java. They have different storage structures and time complexities, making them suitable for different use cases. In this article, we will
3 min read
When to use and avoid array Arrays are fundamental Data Structures in programming, offering a structured way to store and organize elements. However, like any tool, arrays are not a one-size-fits-all solution. Knowing when to embrace or avoid arrays is crucial for efficient and effective software development. In this explorati
3 min read