In: Computer Science
You want to create a good hash function. Which of these properties is the least important for your hash function to have?
Let's say you want to store a collection of unique elements. You would like to be able to search, insert, delete as fast as possible. Which data structure should you use?
Let's say you want your hash table to allow load factors greater than 1. You would use:
Code:
package assignment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
class MyDS
{
ArrayList<Integer> arr; // ArrayList is resizable array
HashMap<Integer, Integer> hash; // A hash where keys are
array elements and vlaues are
public MyDS() { // Constructor for create array and hash
arr = new ArrayList<Integer>();
hash = new HashMap<Integer, Integer>(); }
void insert(int x) {
if (hash.get(x) != null) // If the element is already present then
noting to do
return;
int s = arr.size();// Else put element at the end of array
arr.add(x);
hash.put(x, s); }
void delete(int x) {
Integer index = hash.get(x);
if (index == null)
return;
hash.remove(x); //otherwise remove here
int size = arr.size();
Integer last = arr.get(size-1);
Collections.swap(arr, index, size-1);
arr.remove(size-1);
hash.put(last, index);}// Update hash table for new index of last
element
Integer search(int x) {
return hash.get(x);
}}
public class DS {
public static void main(String args[]) {
MyDS ds = new MyDS();
ds.insert(10);
ds.insert(20);
ds.insert(30);
ds.insert(40);
System.out.println("Search element of index:
"+ds.search(30));
ds.delete(20);
ds.insert(50);
System.out.println("After delete the element:
"+ds.search(50));
}}
Note:***I hope you happy with my answer****If you like my answer please give me upvote*****Thank you....