java 시작
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 'sockMerchant' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER_ARRAY ar
*/
public static int sockMerchant(int n, List<Integer> ar) {
Map<Integer, Integer> sockCnt = new HashMap<>();
for(Integer num: ar){
if(sockCnt.containsKey(num)){
sockCnt.put(num, sockCnt.get(num)+1);
}
else{
sockCnt.put(num, 1);
}
}
Integer ans = 0;
for(Integer key : sockCnt.keySet() ){
Integer pairCnt = sockCnt.get(key) / 2 ;
ans += pairCnt;
}
return ans;
}
}
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 n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> ar = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.sockMerchant(n, ar);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
'algorithm > java' 카테고리의 다른 글
hackerrank/warm up/Repeated String (0) | 2021.06.20 |
---|---|
hackerranck/warm up/jumping on the clouds (0) | 2021.06.13 |
java 개발이 처음일 때 볼 수 있는 에러 (0) | 2021.06.02 |
hackerrank/warm up/Counting Valleys (0) | 2021.05.23 |
java 조건문 사잇값 (0) | 2021.05.18 |