Problem Statement
You will be given a String[] customerNames, containing a list of customer names extracted from a database. Your task is to report the customers that occur more than once in this list, and the number of occurrences for each of the repeated customers.
Your method should return the report as a String[]. Each element in this String[] should be of the form "NAME OCCURS", where NAME is the name of one customer and OCCURS is the number of times his name occurs in customerNames. Sort the result in alphabetical order by customer name.
Definition
- Class:
- CustomerStatistics
- Method:
- reportDuplicates
- Parameters:
- String[]
- Returns:
- String[]
- Method signature:
- String[] reportDuplicates(String[] customerNames)
- (be sure your method is public)
Constraints
- customerNames contains between 1 and 50 elements, inclusive.
- Each element of customerNames contains between 1 and 50 characters, inclusive.
- Each element of customerNames contains uppercase letters ('A'-'Z') only.
Examples
{"JOHN", "BOB", "JOHN", "BILL", "STANLEY", "JOHN"}Returns: {"JOHN 3" }The only repeated name is JOHN, and it occurs three times.{"YETTI", "YETTI", "YETTI", "BIGFOOT", "BIGFOOT"}Returns: {"BIGFOOT 2", "YETTI 3" }Note the sorting order.{"ANDREW", "BILL", "CINDY", "DOH", "ERGH", "FOO", "GOO", "HMPH"}Returns: { }No repeated names this time.{"THEONLYCUSTOMER"}Returns: { }Again, no repeats.{"A", "B", "A", "C", "A", "B", "A", "D", "D", "D"}Returns: {"A 4", "B 2", "D 3" }