Posts

Search

Featured Products

An e-commerce site tracks the purchases made each day. The product that is purchased the most one day is the featured product for the following day. If there is a tie for the product purchased most frequently, those product names are ordered alphabetically ascending and the last name in the list is chosen.

Example

products = [‘redShirt’, ‘greenPants’, ‘redShirt’, ‘orangeShoes’, ‘blackPants’, ‘blackPants’]
greenPants and orangeShoes were purchased once.
redShirt and blackPants were each purchased 2 times on the given day.
After ordering the products alphabetically ascending, redShirt is the last product listed.
redShirt is the featured product for the following day.

Function Description

Complete the function featuredProduct in the editor below.
featuredProduct has the following parameter(s):
    string products[n]:  an array of strings where each represents a purchased product
Returns:
    string: the name of the featured product

Constraints

1 ≤ n ≤ 104

Input Format For Custom Testing

Input from stdin will be processed as follows and passed to the function:
The first line contains an integer n, the number of elements in products.
Each of the n subsequent lines contains a string that describes products[i] where 0 ≤ i < n.

Sample Case 0
Sample Input

STDIN            Function 
-----            ----- 
10           →   products[] size n = 10
yellowShirt  →   products = ['yellowShirt', 'redHat', 'blackShirt', 'bluePants', 'redHat',\
redHat                       'pinkHat', 'blackShirt', 'yellowShirt', 'greenPants', 'greenPants']
blackShirt
bluePants
redHat
pinkHat
blackShirt
yellowShirt
greenPants
greenPants

Sample Output

yellowShirt

Explanation

pinkHat and yellowPants were each purchased 1 time.
yellowShirt, blackShirt, redHat, and greenPants were each purchased 2 times.
yellowShirt is the first product listed after ordering the products alphabetically ascending: blackShirt, greenPants, redHat, yellowShirt
yellowShirt is the featured product.
 

Sample Case 1
Sample Input

STDIN       Function
-----       -----
8          →  products[] size n = 8
greenShirt →  products = ['greenShirt', 'bluePants', 'redShirt', 'blackShoes', 'redPants', 'redPants', 'whiteShirt', 'redShirt']
bluePants
redShirt
blackShoes
redPants
redPants
whiteShirt
redShirt

Sample Output

redShirt

Explanation

greenShirt, bluePants, blackShoes, and whiteShirt were each purchased 1 time.
redShirt and redPants were each purchased 2 times.
redShirt is the last product listed after ordering the products alphabetically ascending: redPants, redShirt.
redShirt is the featured product for the following day.

Solution:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



class Result {

    /*
     * Complete the 'featuredProduct' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING_ARRAY products as parameter.
     */
     static String findWord(String[] arr) 
    { 
  
        HashMap<String, Integer> hs = new HashMap<String, Integer>(); 
    for (int i = 0; i < arr.length; i++) { 
            if (hs.containsKey(arr[i])) { 
                hs.put(arr[i], hs.get(arr[i]) + 1); 
            } 
            else { 
                hs.put(arr[i], 1); 
            } 
        } 
        Set<Map.Entry<String, Integer> > set = hs.entrySet(); 
        String key = ""
        int value = 0
  
        for (Map.Entry<String, Integer> me : set) { 
            if (me.getValue() > value) { 
                value = me.getValue(); 
                key = me.getKey(); 
            } 
        } 
        return key; 
    } 

    public static String featuredProduct(List<String> products) {
    // Write your code here
    String ar[]=new String [products.size()];
    int j=0;
    for (String str : products) 
    {
        ar[j]=str;
        System.out.println(ar[j]);
        j++;
    }
    String sol = findWord(ar);
    return sol;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int productsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> products = IntStream.range(0, productsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        String result = Result.featuredProduct(products);

        bufferedWriter.write(result);
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}