Open In App

Why use an Array to implement a “list” instead of a Hash Table?

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

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 + " ");
}


Output

1 2 3 4 5 




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();


Output

1 2 3 4 5 


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads