0% found this document useful (0 votes)
7 views1 page

Java Word Frequency Analysis Code

The document provides a detailed breakdown of a Java code that processes a string by converting it to lowercase, counting word frequencies, identifying duplicates, and sorting the words. It includes step-by-step explanations of each part of the code, from input handling to output generation. The code serves as a foundational example for common string and array-related coding interview questions using Java Collections.

Uploaded by

Sukanya samant
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Java Word Frequency Analysis Code

The document provides a detailed breakdown of a Java code that processes a string by converting it to lowercase, counting word frequencies, identifying duplicates, and sorting the words. It includes step-by-step explanations of each part of the code, from input handling to output generation. The code serves as a foundational example for common string and array-related coding interview questions using Java Collections.

Uploaded by

Sukanya samant
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Made with Notes

07 Jun 2025 05:24AM

Java code

Absolutely! Let's break down the Java code line by line, so you clearly understand what each step does. ---
Full Annotated Version of the Code import [Link].*; public class CollectionUtils { public static void
🔁
Convert to array of lowercase words String[] words = [Link]().split(" "); // 👉
main(String[] args) { // Step 1: Input string String input = "Java is awesome and Java is powerful"; // Step 2:
This gives: ["java",
"is", "awesome", "and", "java", "is", "powerful"] // Step 3: Create a HashMap to store frequency of each word

appears [Link](word, [Link](word, 0) + 1); // 👉


Map freqMap = new HashMap<>(); for (String word : words) { // Step 4: Count how many times each word
getOrDefault() returns 0 if word not in

//👉
map yet } // Step 5: Print word frequency [Link]("Word Frequency:"); [Link](freqMap);
Output: {java=2, is=2, awesome=1, and=1, powerful=1} // Step 6: Find and store duplicate words Set
duplicates = new HashSet<>(); for ([Link] entry : [Link]()) { if ([Link]() > 1) {
[Link]([Link]()); } } [Link]("Duplicate words:"); [Link](duplicates); // 👉
Output: [java, is] // Step 7: Store unique words (Set automatically removes duplicates) Set unique = new
HashSet<>([Link](words)); [Link]("Unique words:"); [Link](unique); // 👉
Output: [java, is, awesome, and, powerful] // Step 8: Find first non-repeating word using the frequency map for

break; //👉 👉
(String word : words) { if ([Link](word) == 1) { [Link]("First non-repeating word: " + word);
Stop after finding the first one } } // Output: awesome (only occurs once, and comes first) //
Step 9: Sort all words alphabetically List wordList = new ArrayList<>([Link](words));
[Link](wordList); [Link]("Sorted words:"); [Link](wordList); // 👉 Output:

[Link]("Reversed words:"); [Link](wordList); //


awesome, and] } } ---
👉
[and, awesome, is, is, java, java, powerful] // Step 10: Reverse the sorted list [Link](wordList);

🧠 Output: [powerful, java, java, is, is,


Summary of What Each Section Does: Step What It Does 1–2 Takes input and

converts to lowercase words 3–4 Builds a frequency map (word count) 5 Prints frequency of each word 6
Extracts duplicate words (those with count > 1) 7 Collects all unique words 8 Finds first non-repeating word
from left 9 Sorts the words alphabetically 10 Reverses the sorted list --- This code gives you a strong
foundation for handling almost any common String/Array coding interview question using Java Collections.
Let me know if you want a character version of this (for problems like duplicate characters or first non-
repeating character).

You might also like