How To Detect Duplicate Words Inwards Coffee String? [Solution]

Problem :  Write a Java plan to impress the duplicate words from a given contention e.g. if given String is "Java too JavaScript are totally different, JavaScript follows Java" so your plan should impress "Java" too "JavaScript" because those 2 are 2 duplicate words from given String. You demand to consider all cases e.g. given String tin flame hold upwardly null, empty, may or may non incorporate whatsoever duplicate words, but for simplicity, yous tin flame assume that judgement volition e'er inward English linguistic communication too exclusively utilization ASCII characters, alphabets, too numerals, no special character.  It's amend to teach the requirement correct of the work inward the start fifty-fifty if the interviewer doesn't say yous everything. Directly jumping into solution without bespeak a span of questions may non teach good amongst many interviewers who looks for especial oriented candidates.

If yous are practicing these coding problems for an interview, I too propose yous receive got a appear at Cracking the Coding Interview book. It contains 150 Programming Questions too their Solutions, which is proficient plenty to clear most of the beginner too intermediate programming project interviews.

Write a Java plan to impress the duplicate words from a given contention e How to detect duplicate words inward Java String? [Solution]


Solution : In club to detect duplicate words, nosotros initiative off demand to split upwardly the judgement into words. For that, yous tin flame split the String on infinite using a greedy regular expression, so that it tin flame conduct hold multiple white spaces betwixt words. You tin flame utilization the split() method of java.lang.String shape to create that, this method returns an array of words.

Once nosotros listing of words, nosotros tin flame insert them into HashSet. Since HashSet doesn't permit duplicate too its add() method render faux if an object already exists inward HashSet, nosotros tin flame detect all duplicate words. Just loop over array, insert them into HashSet using add() method, depository fiscal establishment tally output of add() method. If add() returns faux so it's a duplicate, impress that give-and-take to the console.

This is too 1 of the top xx String based problems from interviews. You tin flame run across that article to to a greater extent than coding problems based upon String.

One of the follow-up questions of this is how create yous detect a position out of times each duplicate give-and-take has appeared inward a sentence? For example, inward our coding problem, your solution should too impress count of both Java too JavaScript e.g. Java : 2 too JavaScript : 2 because they receive got appeared twice inward a sentence.


You tin flame solve this work yesteryear choosing only about other hash-based information construction similar a hash table, which maintains primal value pair. Java provides several implementation of hash tabular array information construction e.g. HashMap, Hashtable, too ConcurrentHashMap, but for full general purpose, HashMap is proficient enough.

In short, only utilization HashMap instead of HashSet to snuff it along count of duplicate words inward the sentence. This is too similar to the work of finding duplicate characters inward String. Instead of character, yous demand to detect duplicate words, every bit shown here.

Another follow-up enquiry related to this work is how create yous take duplicate words from String inward Java? Which is genuinely the same work of removing duplicate elements from an array? If yous know how to solve that, yous tin flame easily solve this 1 every bit well. If yous human face upwardly whatsoever problem,  see this solution.

Write a Java plan to impress the duplicate words from a given contention e How to detect duplicate words inward Java String? [Solution]


Java Program to detect duplicate words inward String

Here is our solution to the work of finding duplicate words inward a judgement inward Java. I receive got used HashSet to detect duplicates. The fourth dimension complexity of this solution is O(n) because nosotros demand to iterate over all chemical component inward the array. You too demand a buffer of the same size every bit master copy array, hence, the infinite complexity is too O(n), so it may non hold upwardly suitable for a genuinely long String. You demand to a greater extent than retentiveness to detect fifty-fifty a unmarried duplicate give-and-take if your String is huge.

import java.util.Collections; import java.util.HashSet; import java.util.Set;  /**  * Java Program to demonstrate how to detect duplicate words inward String.  */ public class DuplicateWordsInString{      public static void main(String[] args) {         String test = "This judgement contains 2 words, 1 too two";         Set<String> duplicates = duplicateWords(test);         System.out.println("input : " + test);         System.out.println("output : " + duplicates);     }       /**      * Method to detect duplicate words inward a Sentence or String      * @param input String       * @return laid of duplicate words      */     public static Set<String> duplicateWords(String input){                  if(input == null || input.isEmpty()){             return Collections.emptySet();         }         Set<String> duplicates = new HashSet<>();                  String[] words = input.split("\\s+");         Set<String> set = new HashSet<>();                  for(String give-and-take : words){             if(!set.add(word)){                 duplicates.add(word);             }         }         return duplicates;     }           }  Output : input : This judgement contains 2 words, 1 and 2 output : [two] 

From the output it's clear that our plan is working every bit expected, It correct prints that "two" is the exclusively duplicate give-and-take inward given String. Nonetheless, nosotros are going to write only about unit of measurement evidence to farther evidence our solution for dissimilar input values.


JUnit tests

Here is my listing of JUnit evidence shape for our solution. We are going to evidence our solution for empty String, goose egg String, String amongst exclusively duplicates, String without whatsoever duplicates too String which contains multiple spaces betwixt words.  Each JUnit tests 1 input. If your input laid is large so yous tin flame too consider using parameterized JUnit test.

import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue;  import java.util.Collections; import java.util.Set;  import org.junit.Test;  public class DuplicateWordsInStringTest {             @Test     public void testWithEmptyString(){                 Set<String> output = DuplicateWordsInString.duplicateWords("");         assertEquals(Collections.emptySet(), output);     }          @Test     public void testWithNullString(){         Set<String> output = DuplicateWordsInString.duplicateWords(null);         assertEquals(Collections.emptySet(), output);     }          @Test     public void testWithDuplicateString(){         Set<String> output = DuplicateWordsInString.duplicateWords("one 1 one 2 two");         assertTrue(output.contains("one"));         assertTrue(output.contains("two"));         assertTrue(output.size() == 2);     }          @Test     public void testWithOutDuplicates(){         Set<String> output = DuplicateWordsInString.duplicateWords("one 2 three");         assertEquals(Collections.emptySet(), output);     }          @Test     public void testWithMultipleSpaceBetweenWord(){         Set<String> output = DuplicateWordsInString.duplicateWords(" 1   2    3 ");         assertEquals(Collections.emptySet(), output);     }           }


That's all nearly how to detect duplicate words inward a given String inward Java. We receive got used HashSet information construction to solve this work too our solution has fourth dimension too infinite complexity of O(n). For a curious developer, tin flame yous come upwardly up amongst a solution amongst amend fourth dimension too infinite complexity? How nearly a solution amongst fourth dimension complexity inward club of O(k) where k is duplicate words? or O(logN)?

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


Belum ada Komentar untuk "How To Detect Duplicate Words Inwards Coffee String? [Solution]"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel