THINK FIRST·CODE LATER

← All labs

First unique letter with a LinkedHashMap

Problem

Write FirstUnique. For every non-empty input line, count its letters (ignore case, digits, spaces and punctuation) in a LinkedHashMap<Character, Integer> and print the first letter that occurs exactly once, the most frequent letter (the earliest one on a tie) and the number of distinct letters.

For the input

swiss cheese
level
Data Structures

the program prints

swiss cheese
  first unique letter: w
  most frequent: s x4
  distinct letters: 6
level
  first unique letter: v
  most frequent: l x2
  distinct letters: 3
Data Structures
  first unique letter: d
  most frequent: t x3
  distinct letters: 8

What would go wrong with a plain HashMap?

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d5-3-data-1.txt

swiss cheese
level
Data Structures
What your program must print for d5-3-data-1.txt
swiss cheese
  first unique letter: w
  most frequent: s x4
  distinct letters: 6
level
  first unique letter: v
  most frequent: l x2
  distinct letters: 3
Data Structures
  first unique letter: d
  most frequent: t x3
  distinct letters: 8

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d5-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content d5-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.