Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
Creating a hash table using Javascript
Let us set up a simple class that we'll use to define all these methods on. We'll create a container object to store the hash table and create a display function to display the table. Note that for collision resolution, we'll use chaining.The display function takes each entry (hashed value) in the table and prints all pairs associated with it.ExampleWe'll also create a new class on the prototype to store the key-value pairs.class HashTable { constructor() { this.container = []; // Populate the container with empty arrays // which can ...
Read MoreAdd elements to a hash table using Javascript
When adding elements to a hash table the most crucial part is collision resolution. We're going to use chaining for the same. There are other algorithms you can read about here: https://en.wikipedia.org/wiki/Hash_table#Collision_resolutionNow let's look at the implementation. We'll be creating a hash function that'll work on integers only to keep this simple. But a more complex algorithm can be used to hash every object − Exampleput(key, value) { let hashCode = hash(key); for(let i = 0; i < this.container[hashCode].length; i ++) { // Replace the existing value with the given key ...
Read MoreSearch Element in an Javascript Hash Table
We've kind of already implemented this in our put method. Let us look at it again in isolation.Exampleget(key) { let hashCode = hash(key); for(let i = 0; i < this.container[hashCode].length; i ++) { // Find the element in the chain if(this.container[hashCode][i].key === key) { return this.container[hashCode][i]; } } return undefined; }You can test it using.Examplelet ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ht.put(15, 21); ht.put(32, 34); console.log(ht.get(20)); console.log(ht.get(21)); console.log(ht.get(55)); console.log(ht.get(32));OutputThis will give the output.{ key: 20, ...
Read MoreHow to give a limit to the input field in HTML?
The HTML tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively.The max and min attributes are used with number, range, date, datetime, datetime-local, month, time and week input types.ExampleYou can try to run the following code to give a limit to the input field in HTML − HTML input number Mention any number between 1 to 10
Read MoreRemove elements from Javascript Hash Table
To remove elements, we simply need to find them and remove them using a simple splice function call that removes elements in place from an array.Let us look at the implementation of the same − Exampleremove(key) { let hashCode = this.hash(key); for (let i = 0; i < this.container[hashCode].length; i++) { // Find the element in the chain if (this.container[hashCode][i].key === key) { this.container[hashCode].splice(i, 1); return true } } return false; }You can test this using − Examplelet ht = ...
Read MoreHow can I show and hide div on mouse click using jQuery?
To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.ExampleLive Demo $(document).ready(function(){ $('#show').click(function() { $('.menu').toggle("slide"); }); }); Click to Show/ Hide div India US UK Australia
Read MoreLoop through a hash table using Javascript
Now let us create a forEach function that'll allow us to loop over all key-value pairs and call a callback on those values. For this, we just need to loop over each chain in the container and call the callback on the key and value pairs.ExampleforEach(callback) { // For each chain this.container.forEach(elem => { // For each element in each chain call callback on KV pair elem.forEach(({ key, value }) => callback(key, value)); }); }You can test this using.Examplelet ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ...
Read MoreJoining two hash tables in Javascript
Sometimes we need to combine containers together using a join function and get a new container. We'll write a static join method that takes in 2 HashTables and creates a new HashTable with all the values. For the sake of simplicity, we'll let the values from the second one override the values for the first one if there are any keys present in both of them. Examplestatic join(table1, table2) { // Check if both args are HashTables if(!table1 instanceof HashTable || !table2 instanceof HashTable) { throw new Error("Illegal Arguments") } let combo = ...
Read MoreThe HashTable Class in Javascript
Here is the complete implementation of the HashTable class. This could of course be improved by using more efficient data structures and collision resolution algorithms.Exampleclass HashTable { constructor() { this.container = []; // Populate the container with empty arrays // which can be used to add more elements in // cases of collisions for (let i = 0; i < 11; i++) { this.container.push([]); } } display() { this.container.forEach((value, index) => ...
Read MoreHow can I show and hide an HTML element using jQuery?
To hide and show an HTML element using jQuery, use the hide and show() methods. Here, the element is hidden and shown on button click,ExampleLive Demo $(document).ready(function(){ $("#visible").click(function(){ $("p").show(); }); $("#hidden").click(function(){ $("p").hide(); }); }); This text will show on clicking "Show element" button, and hide on clicking "Hide element" button. Hide element Show element
Read More