mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
@ -33,7 +33,8 @@ public class HashMapCuckooHashing {
|
||||
}
|
||||
|
||||
/**
|
||||
* The 2 Hash Functions takes a given key and finds an index based on its data, 2 distinctive ways to minimize collisions
|
||||
* The 2 Hash Functions takes a given key and finds an index based on its data, 2 distinctive
|
||||
* ways to minimize collisions
|
||||
*
|
||||
* @param key the desired key to be converted
|
||||
* @return int an index corresponding to the key
|
||||
@ -57,10 +58,10 @@ public class HashMapCuckooHashing {
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts the key into the hash map by wrapping it as an Integer object, then uses while loop to insert new key
|
||||
* if desired place is empty, return.
|
||||
* if already occupied, continue while loop over the new key that has just been pushed out.
|
||||
* if while loop continues more than Thresh, rehash table to new size, then push again.
|
||||
* inserts the key into the hash map by wrapping it as an Integer object, then uses while loop
|
||||
* to insert new key if desired place is empty, return. if already occupied, continue while loop
|
||||
* over the new key that has just been pushed out. if while loop continues more than Thresh,
|
||||
* rehash table to new size, then push again.
|
||||
*
|
||||
* @param key the desired key to be inserted in the hash map
|
||||
*/
|
||||
@ -70,26 +71,19 @@ public class HashMapCuckooHashing {
|
||||
int hash, loopCounter = 0;
|
||||
|
||||
if (isFull()) {
|
||||
System.out.println(
|
||||
"Hash table is full, lengthening & rehashing table"
|
||||
);
|
||||
System.out.println("Hash table is full, lengthening & rehashing table");
|
||||
reHashTableIncreasesTableSize();
|
||||
}
|
||||
|
||||
if (checkTableContainsKey(key)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Key already inside, no duplicates allowed"
|
||||
);
|
||||
throw new IllegalArgumentException("Key already inside, no duplicates allowed");
|
||||
}
|
||||
|
||||
while (loopCounter <= thresh) {
|
||||
loopCounter++;
|
||||
hash = hashFunction1(key);
|
||||
|
||||
if (
|
||||
(buckets[hash] == null) ||
|
||||
Objects.equals(buckets[hash], AVAILABLE)
|
||||
) {
|
||||
if ((buckets[hash] == null) || Objects.equals(buckets[hash], AVAILABLE)) {
|
||||
buckets[hash] = wrappedInt;
|
||||
size++;
|
||||
checkLoadFactor();
|
||||
@ -116,16 +110,14 @@ public class HashMapCuckooHashing {
|
||||
buckets[hash] = wrappedInt;
|
||||
wrappedInt = temp;
|
||||
}
|
||||
System.out.println(
|
||||
"Infinite loop occurred, lengthening & rehashing table"
|
||||
);
|
||||
System.out.println("Infinite loop occurred, lengthening & rehashing table");
|
||||
reHashTableIncreasesTableSize();
|
||||
insertKey2HashTable(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* creates new HashMapCuckooHashing object, then inserts each of the elements in the previous table to it with its new hash functions.
|
||||
* then refers current array to new table.
|
||||
* creates new HashMapCuckooHashing object, then inserts each of the elements in the previous
|
||||
* table to it with its new hash functions. then refers current array to new table.
|
||||
*
|
||||
*/
|
||||
public void reHashTableIncreasesTableSize() {
|
||||
@ -164,9 +156,7 @@ public class HashMapCuckooHashing {
|
||||
size--;
|
||||
return;
|
||||
}
|
||||
throw new IllegalArgumentException(
|
||||
"Key " + key + " already inside, no duplicates allowed"
|
||||
);
|
||||
throw new IllegalArgumentException("Key " + key + " already inside, no duplicates allowed");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -177,9 +167,7 @@ public class HashMapCuckooHashing {
|
||||
if ((buckets[i] == null) || Objects.equals(buckets[i], AVAILABLE)) {
|
||||
System.out.println("Bucket " + i + ": Empty");
|
||||
} else {
|
||||
System.out.println(
|
||||
"Bucket " + i + ": " + buckets[i].toString()
|
||||
);
|
||||
System.out.println("Bucket " + i + ": " + buckets[i].toString());
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
@ -202,11 +190,9 @@ public class HashMapCuckooHashing {
|
||||
if (Objects.equals(buckets[hash], wrappedInt)) return hash;
|
||||
|
||||
hash = hashFunction2(key);
|
||||
if (
|
||||
!Objects.equals(buckets[hash], wrappedInt)
|
||||
) throw new IllegalArgumentException(
|
||||
"Key " + key + " not found in table"
|
||||
); else {
|
||||
if (!Objects.equals(buckets[hash], wrappedInt))
|
||||
throw new IllegalArgumentException("Key " + key + " not found in table");
|
||||
else {
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
@ -218,16 +204,8 @@ public class HashMapCuckooHashing {
|
||||
* @return int the index where the key is located
|
||||
*/
|
||||
public boolean checkTableContainsKey(int key) {
|
||||
return (
|
||||
(
|
||||
buckets[hashFunction1(key)] != null &&
|
||||
buckets[hashFunction1(key)].equals(key)
|
||||
) ||
|
||||
(
|
||||
buckets[hashFunction2(key)] != null &&
|
||||
buckets[hashFunction2(key)] == key
|
||||
)
|
||||
);
|
||||
return ((buckets[hashFunction1(key)] != null && buckets[hashFunction1(key)].equals(key))
|
||||
|| (buckets[hashFunction2(key)] != null && buckets[hashFunction2(key)] == key));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -237,10 +215,7 @@ public class HashMapCuckooHashing {
|
||||
public double checkLoadFactor() {
|
||||
double factor = (double) size / tableSize;
|
||||
if (factor > .7) {
|
||||
System.out.printf(
|
||||
"Load factor is %.2f , rehashing table\n",
|
||||
factor
|
||||
);
|
||||
System.out.printf("Load factor is %.2f , rehashing table\n", factor);
|
||||
reHashTableIncreasesTableSize();
|
||||
}
|
||||
return factor;
|
||||
|
@ -15,9 +15,7 @@ import java.util.Map;
|
||||
public class Intersection {
|
||||
|
||||
public static List<Integer> intersection(int[] arr1, int[] arr2) {
|
||||
if (
|
||||
arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0
|
||||
) {
|
||||
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Map<Integer, Integer> cnt = new HashMap<>(16);
|
||||
@ -34,5 +32,6 @@ public class Intersection {
|
||||
return res;
|
||||
}
|
||||
|
||||
private Intersection() {}
|
||||
private Intersection() {
|
||||
}
|
||||
}
|
||||
|
@ -20,31 +20,27 @@ public class Main {
|
||||
choice = In.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
{
|
||||
System.out.println("Enter the Key: ");
|
||||
key = In.nextInt();
|
||||
h.insertHash(key);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
System.out.println("Enter the Key delete: ");
|
||||
key = In.nextInt();
|
||||
h.deleteHash(key);
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
System.out.println("Print table");
|
||||
h.displayHashtable();
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
In.close();
|
||||
return;
|
||||
}
|
||||
case 1: {
|
||||
System.out.println("Enter the Key: ");
|
||||
key = In.nextInt();
|
||||
h.insertHash(key);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
System.out.println("Enter the Key delete: ");
|
||||
key = In.nextInt();
|
||||
h.deleteHash(key);
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
System.out.println("Print table");
|
||||
h.displayHashtable();
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
In.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,59 +24,41 @@ public class MainCuckooHashing {
|
||||
choice = In.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
{
|
||||
System.out.println("Enter the Key: ");
|
||||
key = In.nextInt();
|
||||
h.insertKey2HashTable(key);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
System.out.println("Enter the Key delete: ");
|
||||
key = In.nextInt();
|
||||
h.deleteKeyFromHashTable(key);
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
System.out.println("Print table:\n");
|
||||
h.displayHashtable();
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
In.close();
|
||||
return;
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
System.out.println(
|
||||
"Enter the Key to find and print: "
|
||||
);
|
||||
key = In.nextInt();
|
||||
System.out.println(
|
||||
"Key: " +
|
||||
key +
|
||||
" is at index: " +
|
||||
h.findKeyInTable(key) +
|
||||
"\n"
|
||||
);
|
||||
break;
|
||||
}
|
||||
case 6:
|
||||
{
|
||||
System.out.printf(
|
||||
"Load factor is: %.2f\n",
|
||||
h.checkLoadFactor()
|
||||
);
|
||||
break;
|
||||
}
|
||||
case 7:
|
||||
{
|
||||
h.reHashTableIncreasesTableSize();
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
System.out.println("Enter the Key: ");
|
||||
key = In.nextInt();
|
||||
h.insertKey2HashTable(key);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
System.out.println("Enter the Key delete: ");
|
||||
key = In.nextInt();
|
||||
h.deleteKeyFromHashTable(key);
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
System.out.println("Print table:\n");
|
||||
h.displayHashtable();
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
In.close();
|
||||
return;
|
||||
}
|
||||
case 5: {
|
||||
System.out.println("Enter the Key to find and print: ");
|
||||
key = In.nextInt();
|
||||
System.out.println("Key: " + key + " is at index: " + h.findKeyInTable(key) + "\n");
|
||||
break;
|
||||
}
|
||||
case 6: {
|
||||
System.out.printf("Load factor is: %.2f\n", h.checkLoadFactor());
|
||||
break;
|
||||
}
|
||||
case 7: {
|
||||
h.reHashTableIncreasesTableSize();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +1,32 @@
|
||||
package com.thealgorithms.datastructures.hashmap.hashing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
/*
|
||||
This class finds the majority element(s) in an array of integers.
|
||||
A majority element is an element that appears more than or equal to n/2 times, where n is the length of the array.
|
||||
A majority element is an element that appears more than or equal to n/2 times, where n is the length
|
||||
of the array.
|
||||
*/
|
||||
public class MajorityElement {
|
||||
/*
|
||||
This method returns the majority element(s) in the given array of integers.
|
||||
@param nums: an array of integers
|
||||
@return a list of majority elements
|
||||
*/
|
||||
public static List<Integer> majority(int[] nums){
|
||||
HashMap<Integer,Integer> numToCount = new HashMap<>();
|
||||
/*
|
||||
This method returns the majority element(s) in the given array of integers.
|
||||
@param nums: an array of integers
|
||||
@return a list of majority elements
|
||||
*/
|
||||
public static List<Integer> majority(int[] nums) {
|
||||
HashMap<Integer, Integer> numToCount = new HashMap<>();
|
||||
int n = nums.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (numToCount.containsKey(nums[i])){
|
||||
numToCount.put(nums[i],numToCount.get(nums[i])+1);
|
||||
if (numToCount.containsKey(nums[i])) {
|
||||
numToCount.put(nums[i], numToCount.get(nums[i]) + 1);
|
||||
} else {
|
||||
numToCount.put(nums[i],1);
|
||||
numToCount.put(nums[i], 1);
|
||||
}
|
||||
}
|
||||
List<Integer> majorityElements = new ArrayList<>();
|
||||
for (int key: numToCount.keySet()) {
|
||||
if (numToCount.get(key) >= n/2){
|
||||
for (int key : numToCount.keySet()) {
|
||||
if (numToCount.get(key) >= n / 2) {
|
||||
majorityElements.add(key);
|
||||
}
|
||||
}
|
||||
|
@ -19,5 +19,4 @@ public abstract class Map<Key, Value> {
|
||||
protected int hash(Key key, int size) {
|
||||
return (key.hashCode() & Integer.MAX_VALUE) % size;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user