paizaラーニング　データセット選択メニュー 商品の検索 Java 解答

# STEP: 8 数値の出現率
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step1

## 解答
```java:step8.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        int[] occurrences = new int[10];
        for(int i = 0; i < n; i++){
            int number = sc.nextInt();
            occurrences[number]++;
        }
        
        for(int i = 0; i < occurrences.length; i++){
            if(i == occurrences.length - 1 ){
                System.out.print(occurrences[i]);
            } else {
                System.out.print(occurrences[i] + " ");
            }
        }
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/b3c944c1-335e-b766-ce72-8c1f9bd521b7.png)

# STEP: 9 英小文字の出現率

## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step2

## 解答
```java:step9.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        String line = sc.nextLine();
        char[] charArrasys = line.toCharArray();
        
        int[] occurrences = new int[26];
        for(int i = 0; i < n; i++){
            
            int unicode = Character.getNumericValue(charArrasys[i]);
            occurrences[unicode-10]++;
        }
        
        for(int i = 0; i < occurrences.length; i++){
            if(i < occurrences.length-1 ){
                System.out.print(occurrences[i] + " ");
            } else {
                System.out.println(occurrences[i]);
            }
        }
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/3e7702e1-a2f7-4232-a513-197abc6a1905.png)

# STEP: 10 文字列の出現率
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step3

## 解答
```java:step10.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Map<String, Integer> occurrences = new HashMap <String, Integer>();
        Scanner sc = new Scanner(System.in);

         
        int n = sc.nextInt();
        List<String> list = new ArrayList<String>(); 
        
        for(int i = 0; i < n; i++){
            String letter = sc.next();
            if(occurrences.containsKey(letter)){
                int number = occurrences.get(letter);
                number++;
                occurrences.replace(letter, number);
            } else {
                occurrences.put(letter, 1);
                list.add(letter);
            } 
        }
        
        Collections.sort(list);
        for (int i = 0; i < list.size(); i++){
            System.out.println(list.get(i) + " " + occurrences.get(list.get(i)));
        }
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/c0c9ed95-e86f-12b0-ecc5-c2f47d4a6702.png)

# STEP: 11 価格の算出
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step4

## 解答
```java:step11.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        // 価格表の作成
        Map<String, Integer> priceList = new HashMap<String, Integer>();
        for(int i = 0; i < n; i ++){
            String productName = sc.next();
            int price = sc.nextInt();
            priceList.put(productName, price);
        }
        
        // 価格の表示
        for(int i = 0; i < m; i++){
            String productName = sc.next();
            
            if(priceList.containsKey(productName)){
                System.out.println(priceList.get(productName));
            }else{
                System.out.println("-1");
            }
        }
        
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/ed494832-69d6-8bcc-025e-8f407f7b35d1.png)


# FINAL問題 商品の検索
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__dict_boss

## 解答
```java:final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int q = sc.nextInt();
        
        Map<String, Integer> productList = new HashMap<String, Integer>();
        
        // 商品表作成
        for(int i = 0; i < n; i++){
            String productName = sc.next();
            if(productList.get(productName) == null){
                productList.put(productName, i+1);   
            }
        }
        
        // 商品検索
        for(int i = 0; i < q; i++){
            String searchProduct = sc.next();
            if(productList.containsKey(searchProduct)){
                System.out.println(productList.get(searchProduct)); 
            }else{
                System.out.println("-1"); 
            }
        }
        
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/d90c63e9-e5d5-4673-f71b-615f143fff33.png)