HashMap & HashSet
493

*HashMap & HashSet
image

When I spot problems related to duplicate values, my mind goes full Bollywood: "Mere Karan (HashMap) Arjun (HashSet) ayenge!" 😂

Let's discuss about this data Structures..

HashMap :

It is the Data Structure, It save the data in key -> value pair. you can access the value by key.
When you again add the same key it not add new key->value pair it change the value according the key.

We most of time use map for save frequency of perticuler string , charcters or integers or for other use.

It has many Methods like : put(key, value), get(key), containsKey(key), containsValue(value), remove(key), size(), isEmpty(), keySet(), values(), entrySet() etc.

HashSet:

A HashSet is a Data Structure in Java that stores elements without duplicates. It ensures uniqueness of elements and does not store the duplicate values.
HashSet is useful when you need a collection that doesn't preserve the order of elements and efficiently handles uniqueness.

We use HashSet in case where you need to maintain a unique set of elements, for keeping track of distinct items in a set.

It has many Methods like : add(element), remove(element), contains(element)
, size(), isEmpty(), clear() etc.

But now honestly when i do leetcode then most of time when in constraints number limits are small like 100 or 1000
so we simply make that size of array. In Example when we do operation on alphabet characters so we simply make 26 size Array
. or when we do operation on digits we simply make 10 size of Array.

If you have more useful thoughts for me and others you can share your thoughts in comment down..📌

Thank You..!

#hashset #hashmap #datastructuresandalgorithms #ignorespallingmistake #omjethva #java

Comments (0)