paizaラーニング データセット選択メニュー 集合の結合 Java 解答

# STEP: 13 集合の探索
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__set_step1

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


public class Main {
    
    private static final String YES = "Yes";
    private static final String NO = "No";
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int b = sc.nextInt();
        
        String result = NO;
        
        for(int i = 0; i < n; i++){
            if(b == sc.nextInt()){
                result = YES;
                break;
            }
        }
        
        System.out.println(result);
        
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/12caf019-b7e4-9095-fdfe-37e493dee335.png)

# STEP: 14 重複の削除
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__set_step2
## 解答
```java:step14java
import java.util.*;


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

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        List<Integer> list = new ArrayList<Integer>();
        
        for(int i = 0; i < n; i++){
            int number = sc.nextInt();
            if(!list.contains(number)){
                list.add(number);
            }
        }
        
        for(int i = 0; i < list.size(); i++){
            if(i<list.size()-1){
                System.out.print(list.get(i) + " ");
            } else{
                System.out.print(list.get(i));
            }
        }
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/ff004cd3-c942-17e3-669e-a9662bb8064c.png)

# STEP: 15 重複の判定 1
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__set_step3
## 解答
```java:step15.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        // １回目の要素は、探索対象外の為、要素を先にリストに入れる
        list.add(sc.nextInt());
        
        // 要素が1つ減っているのでn-1とする
        for(int i = 0; i < n-1; i++){
            int number = sc.nextInt();
            if(list.contains(number)){
                System.out.println("Yes");
            }else{
                System.out.println("No");
                list.add(number);
            }
        }
        
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/2a78514a-cb94-6bec-d2d8-2d2a7b29db68.png)

# STEP: 16 重複の判定 2
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__set_step4

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        List<Long> list = new ArrayList<Long>();
        list.add(sc.nextLong()); 
        
        for(int i = 0; i < n-1; i++){
            Long number = sc.nextLong();
            if(list.contains(number)){
                 System.out.println("Yes");
            } else {
                 System.out.println("No");
                 list.add(number);
            }
        }
       
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/a76d8b2b-5e7e-ae34-0801-67cd244f02fb.png)

# FINAL問題 集合の結合
## 問題
https://paiza.jp/works/mondai/data_structure/data_structure__set_boss

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


public class Main {
    public static void main(String[] args) {
        
        List<Integer> list = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        for(int i = 0; i < n*2; i++){
            int number = sc.nextInt();
            if(!list.contains(number)){
                list.add(number);
            }    
        }
        
        Collections.sort(list);
        
        for(int i = 0; i < list.size(); i++){
            if(i < list.size()-1){
                System.out.print(list.get(i) + " ");
            } else {
                System.out.println(list.get(i));
            }
        }
    }
}
```
## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/0f6c0690-fd5e-fddb-a7d4-2a2afef4de11.png)