I am trying to store data from API through backend and I am always confused when to use hash and when to use arrays
I just created array, but I want to filter array based on specific key value
let course_ids = []
course_ids = this.state.courses.map( (course,index) => {
if (index < this.state.index_value) {
return course.id
}
})
I am getting course Ids, now i have to sort another data in which key value pairs are course ids using this course_ids I have from courses data.
The biggest advantage in using hashtables is that it is much faster to search data afterwards.
If you try to search something in an array, supposing the data is not sorted, the Big O (time complexity) will be O(N), where N is the size of the array. In other words, you have to iterate through each element of the array (N iterations). If the array has 100 or 1000 elements, it should be ok. The problem is when you have arrays with too much data: 1000000 elements for example.
If you try to search using a key in a hashtable, the time complexity will be O(1) (average case). In other words, it does not matter if the hashtable has 1000 or 1000000 elements - it will always take only one iteration to return the data you are looking for, if the hashtable is well-dimensioned.
Take a look at https://en.wikipedia.org/wiki/Hash_table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With