Step-by-step explanation:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;
public class DuplicateTitles {
public static void main(String[] args) {
// specify input and output file names
String inputFile = "bookTitles.inp";
String outputFile = "duplicateTitles.out";
// read book titles from input file and count occurrences of each title
HashMap<String, Integer> titleCounts = new HashMap<String, Integer>();
try {
Scanner scanner = new Scanner(new File(inputFile));
while (scanner.hasNextLine()) {
String title = scanner.nextLine();
if (titleCounts.containsKey(title)) {
titleCounts.put(title, titleCounts.get(title) + 1);
} else {
titleCounts.put(title, 1);
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// write duplicate titles to output file
try {
PrintWriter writer = new PrintWriter(new File(outputFile));
for (String title : titleCounts.keySet()) {
if (titleCounts.get(title) > 1) {
writer.println(title);
}
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
The program first declares the names of the input and output files. It then reads in the book titles from the input file using a Scanner object and stores them in a HashMap called titleCounts. For each title, the program checks if it has already been encountered and updates its count accordingly.
Once all titles have been read, the program writes the duplicate titles to the output file using a PrintWriter object. The program iterates through the titleCounts map, and for each title that has a count greater than one, it writes the title to the output file.
This program is efficient because it only stores one copy of each title in memory, regardless of how many times it appears in the input file. Additionally, it only writes duplicate titles once to the output file, as specified in the prompt.