• Latest
Arrays and Hashing – DZone

Arrays and Hashing – DZone

December 21, 2022
iPhone 14 Review: Repackaging 101!

iPhone 14 Review: Repackaging 101!

March 25, 2023

Recapping All the Best Drama – The TouchArcade Show #552 – TouchArcade

March 25, 2023
How ONE Company Keeps The Internet Running – AWS Explained

How ONE Company Keeps The Internet Running – AWS Explained

March 25, 2023
Top 5 Data Streaming Trends for 2023

Top 5 Data Streaming Trends for 2023

March 25, 2023
🔥 UNLIMITED AFK XP🔥  Fortnite Creative Map Glitch – Chapter 4 Season 2 *NOT PATCHED*

🔥 UNLIMITED AFK XP🔥 Fortnite Creative Map Glitch – Chapter 4 Season 2 *NOT PATCHED*

March 25, 2023
10 Things to Know When Using SHACL With GraphDB

10 Things to Know When Using SHACL With GraphDB

March 25, 2023
OPPO Find X6 Pro Review – The Photography KING!

OPPO Find X6 Pro Review – The Photography KING!

March 25, 2023
LEGO 2K Drive Will Include Real Money Transactions

LEGO 2K Drive Will Include Real Money Transactions

March 25, 2023
Carl Pei imitating youtubers | MKBHD | Mrwhosetheboss | JerryRigEverything | Technical Guruji

Carl Pei imitating youtubers | MKBHD | Mrwhosetheboss | JerryRigEverything | Technical Guruji

March 25, 2023
When Does The 3DS And Wii U eShop Close? Nintendo eShop Closure Guide

When Does The 3DS And Wii U eShop Close? Nintendo eShop Closure Guide

March 25, 2023
Samsung may launch a Tri-Fold device this year, the S23 FE isn’t happening

Samsung may launch a Tri-Fold device this year, the S23 FE isn’t happening

March 25, 2023
Sonic Origins Plus Will Apparently Fix Some Pesky Bugs

Sonic Origins Plus Will Apparently Fix Some Pesky Bugs

March 25, 2023
Advertise with us
Saturday, March 25, 2023
Bookmarks
  • Login
  • Register
GetUpdated
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

Arrays and Hashing – DZone

December 21, 2022
in Software Development
Reading Time:7 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


In this article, we will discuss some of the most popular algorithm problems using arrays and hashing approaches. Some of these problems I received during interviews. 

Let’s start with a problem:

Contains Duplicate

Contains Duplicate

Description:

Given an integer array nums, return true if any value appears at least twice 
in the array, and return false if every element is distinct.

Solution:

What if we add an additional data structure like a HashSet and put elements inside? If we have the same elements in Set before insert, we will return true, and that is it. 

So simple, isn’t it?

    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        
        for(int n : nums){
            if(set.contains(n)){
                return true;
            } else {
                set.add(n);
            }
        }
        return false;
    }

Moving on to our next task :

Valid Anagram

Valid Anagram

Description:

Given two strings s and t, return true if t is an anagram of s, 
and false otherwise. An Anagram is a word or phrase formed by rearranging 
the letters of a different word or phrase, typically using all the original 
letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

Solution:

First of all, we should understand what an anagram is. Two words will be anagrams only if they have the same characters. That means that we should compare characters. Characters can be in a different order. We can use a few approaches how to handle it. In the first variant, we can sort characters in each word and then compare them. Or we can create a HashMap and, for one word, add characters, and for another, substruct them. Below is the variant with the sorting algorithm.

    public boolean isAnagram(String s, String t) {
        if(s == null && t == null){
            return true;
        } else if(s == null || t == null){
            return false;
        }
        if(s.length() != t.length()){
            return false;
        }
        char[] sCh = s.toCharArray();
        char[] tCh = t.toCharArray();
        Arrays.sort(sCh);
        Arrays.sort(tCh);
        
        for(int i = 0; i < s.length(); i ++){
            if(sCh[i] != tCh[i]){
                return false;
            }
        }
        return true;
    }

Is it clear? Please, let me know in the comments. 

Our next problem:

Two Sum

Two Sum

Description:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Solution:

This is one of the basic Hash problems. Let’s find a brut force solution. We can prepare two for each loop, and iterate over elements and compare their sums. It works, but the time complexity will be O(N^2), and it could be very, very slow. 

But what if, instead of the second loop, we save all previous elements into HashMap? Will it be checked with current elements? For example, we have array [3,3] and target = 6. In the first iteration, we will put into map 3 as the key and 0(index) as the value. And then, on the next iteration, we check the map with target - cur

In our case, it will be 6 – 3 = 3. We have to pair it in our map with element 3 and map it to get the response. 

Let’s take a look at the code:

    public int[] twoSum(int[] nums, int target) {
        int[] rez = new int[2];
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++){
            int rest = target - nums[i];
            if(map.containsKey(rest)){
                rez[0] = map.get(rest);
                rez[1] = i;
                return rez;
            } else {
                map.put(nums[i], i);
            }
        }
        return rez;
     }

For some of you, these problems may look easy, but not for me. I spent a lot of time trying to find a correct solution. 

Now we will look at the hardest problem in this article:

Group Anagrams

Group Anagrams

Description:

Given an array of strings strs, group the anagrams together. 
You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters 
of a different word or phrase, 
typically using all the original letters exactly once.

Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Example 2:

Input: strs = [""]
Output: [[""]]

Example 3:

Input: strs = ["a"]
Output: [["a"]]

Solution:

Do you remember the previous problem with Anagrams? I want to use the same approach. We remember that anagrams are words with the same characters, and the same characters count. What if we sort characters in the word and create a string from it? For example, we have [nat, tna]. We sort “nat” and receive “ant.” We sort “tan” and again receive “ant.” We can sort and put words into a map. And the key will be a sorted string, and the value will be the original word. Smart, isn’t it?

Time to look at the code:

 public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();

        for (String s : strs) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String sorted = String.valueOf(chars);
            if (map.containsKey(sorted)) {
                map.get(sorted).add(s);
            } else {
                List<String> list = new ArrayList<>();
                list.add(s);
                map.put(sorted, list);
            }
        }

        return new ArrayList<>(map.values());
    }

I hope you are enjoying this topic. Next time, I’m going to solve more complicated topics. 

Feel free to add your thoughts in the comments. I really appreciate your time and want to hear your feedback.



Source link

ShareSendTweet
Previous Post

Teenage Mutant Ninja Turtles: The Cowabunga Collection Switch Icon To Be Updated In Next Patch

Next Post

Elder Scrolls Online Outlines Vision For Game’s Combat And How To Make It Feel Elder Scroll-y

Related Posts

Top 5 Data Streaming Trends for 2023

March 25, 2023
0
0
Top 5 Data Streaming Trends for 2023
Software Development

Data streaming is one of the most relevant buzzwords in tech to build scalable real-time applications in the cloud and...

Read more

10 Things to Know When Using SHACL With GraphDB

March 25, 2023
0
0
10 Things to Know When Using SHACL With GraphDB
Software Development

Today I have one of those moments where I am absolutely sure if I do not write this down, I...

Read more
Next Post
Elder Scrolls Online Outlines Vision For Game’s Combat And How To Make It Feel Elder Scroll-y

Elder Scrolls Online Outlines Vision For Game’s Combat And How To Make It Feel Elder Scroll-y

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?