TopCoder problem "BestSeller" used in TCHS SRM 3 (Division I Level One)



Problem Statement

    

After selling goods all day, a salesman would like to determine the most desirable item in his inventory. You are given a String[] items, each element of which represents a single item that was sold during the day. Return the item that was sold the most number of times. In case of a tie, return the item that comes first alphabetically.

 

Definition

    
Class:BestSeller
Method:findBestSeller
Parameters:String[]
Returns:String
Method signature:String findBestSeller(String[] items)
(be sure your method is public)
    
 

Constraints

-items will contain between 1 and 50 elements, inclusive.
-Each element of items will contain between 1 and 50 characters, inclusive.
-Each element of items will contain only lowercase letters ('a'-'z').
 

Examples

0)
    
{"table", "chair", "table", "table", "lamp", "door", "lamp", "table", "chair"}
Returns: "table"
The salesman sold four "table"s, two "chair"s, two "lamp"s, and one "door". The "table" is his best-selling item.
1)
    
{"a", "a", "a", "b", "b", "b"}
Returns: "a"
There is a tie between "a" and "b", "a" is returned, because it comes first alphabetically.
2)
    
{"icecream", "peanuts", "peanuts", "chocolate", "candy", "chocolate", "icecream", "apple"}
Returns: "chocolate"
The salesman sold two of each of these items: "icecream", "peanuts", and "chocolate". Since there is a tie, "chocolate" is returned because it comes first alphabetically. "apple" comes even earlier, but only tied items are considered.
3)
    
{"soul"}
Returns: "soul"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6440

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10021&pm=6440

Writer:

gevak

Testers:

PabloGilberto , brett1479 , radeye , Olexiy

Problem categories:

Simple Search, Iteration