Thursday, December 12, 2019

How To Honour Duplicate Characters On String - Coffee Programming Problems

Today's programming do is to write a plan to honour repeated characters inwards a String. For example, if given input to your plan is "Java", it should impress all duplicates characters, i.e. characters look to a greater extent than than 1 time inwards String together with their count e.g. a = 2 because grapheme 'a' has appeared twice inwards String "Java". This is too a really pop coding interrogation on the diverse score of Java interviews together with written test, where yous demand to write code. On difficulty level, this interrogation is at par amongst prime numbers or Fibonacci series. I personally similar this do because it gives beginners an chance to familiar amongst the concept of Map information structure, which allows yous shop mappings inwards the cast of fundamental together with value. Since Map is heavily used inwards whatever venture Java application, skilful noesis of this information construction is highly desirable amidst whatever score of Java programmers.


By the way, in that place are a twain of variants of this problem, which yous may desire to await earlier going for an interview. Sometimes an interviewer volition inquire yous to read a file together with impress all duplicate characters together with their count, substance logic volition stay same, all yous demand to create is demonstrate how much yous know virtually File IO inwards Java e.g. streaming file if it's really large rather than reading the whole file inwards memory.




Java Program to honour Repeated Characters of String

The measure means to solve this employment is to larn the grapheme array from String, iterate through that together with construct a Map amongst grapheme together with their count. Then iterate through that Map together with impress characters which accept appeared to a greater extent than than once. So yous truly demand 2 loops to create the job, the get-go loop to construct the map together with bit loop to impress characters together with counts.

If yous await at below example, in that place is entirely 1 static method called printDuplicateCharacters(), which does both this job. We get-go got the grapheme array from String past times calling toCharArray().

Next nosotros are using HashMap to shop characters together with their count. We use containsKey() method to depository fiscal establishment tally if key, which is a grapheme already exists or not, if already exists nosotros larn the former count from HashMap by calling get() method together with shop it dorsum subsequently incrementing it past times 1.

loop through Map together with depository fiscal establishment tally each entry, if count, which is the value of Entry is greater than 1, together with therefore that grapheme has occurred to a greater extent than than once. You tin directly impress duplicate characters or create whatever yous desire amongst them.

import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set;  /** * Java Program to honour duplicate characters inwards String. * * * @author http://java67.blogspot.com */ public class FindDuplicateCharacters{      public static void main(String args[]) {         printDuplicateCharacters("Programming");         printDuplicateCharacters("Combination");         printDuplicateCharacters("Java");     }      /*      * Find all duplicate characters inwards a String together with impress each of them.      */     public static void printDuplicateCharacters(String word) {         char[] characters = word.toCharArray();          // construct HashMap amongst grapheme together with set out of times they look inwards String         Map<Character, Integer> charMap = new HashMap<Character, Integer>();         for (Character ch : characters) {             if (charMap.containsKey(ch)) {                 charMap.put(ch, charMap.get(ch) + 1);             } else {                 charMap.put(ch, 1);             }         }          // Iterate through HashMap to impress all duplicate characters of String         Set<Map.Entry<Character, Integer>> entrySet = charMap.entrySet();         System.out.printf("List of duplicate characters inwards String '%s' %n", word);         for (Map.Entry<Character, Integer> entry : entrySet) {             if (entry.getValue() > 1) {                 System.out.printf("%s : %d %n", entry.getKey(), entry.getValue());             }         }     }  }  Output List of duplicate characters inwards String 'Programming' g : 2 r : 2 1000 : 2 List of duplicate characters inwards String 'Combination' n : 2 o : 2 i : 2 List of duplicate characters inwards String 'Java'


That's all on how to honour duplicate characters inwards a String. Next fourth dimension if this interrogation is asked to yous inwards a programming undertaking interview, yous tin confidently write a solution together with tin explicate them. Remember this interrogation is too asked equally write a Java plan to honour repeated characters of a given String, therefore don't larn confused yourself inwards wording, the algorithm volition stay same.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures together with Algorithms: Deep Dive Using Java
Algorithms together with Data Structures - Part 1 together with 2

No comments:

Post a Comment