From 6b429497f9d5f45a0fbc7006e5cfce42e9266d51 Mon Sep 17 00:00:00 2001 From: ABHISHEK SINGH <115360384+abhishek2s@users.noreply.github.com> Date: Mon, 28 Oct 2024 01:04:08 +0530 Subject: [PATCH] Create hashing.java code of hasing in java --- hashing.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 hashing.java diff --git a/hashing.java b/hashing.java new file mode 100644 index 0000000000..ca90481a2e --- /dev/null +++ b/hashing.java @@ -0,0 +1,44 @@ +// Java program to create HashMap from an array +// by taking the elements as Keys and +// the frequencies as the Values + +import java.util.*; + +class GFG { + + // Function to create HashMap from array + static void createHashMap(int arr[]) + { + // Creates an empty HashMap + HashMap hmap = new HashMap(); + + // Traverse through the given array + for (int i = 0; i < arr.length; i++) { + + // Get if the element is present + Integer c = hmap.get(arr[i]); + + // If this is first occurrence of element + // Insert the element + if (hmap.get(arr[i]) == null) { + hmap.put(arr[i], 1); + } + + // If elements already exists in hash map + // Increment the count of element by 1 + else { + hmap.put(arr[i], ++c); + } + } + + // Print HashMap + System.out.println(hmap); + } + + // Driver method to test above method + public static void main(String[] args) + { + int arr[] = { 10, 34, 5, 10, 3, 5, 10 }; + createHashMap(arr); + } +}