Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 배열의 원소는 1~13
- // 이 배열에서 2번이상 페어로 나오는 숫자들을 비교해서 둘중 어느쪽이 더 큰값인지
- // 양쪽 배열에 모두 페어가 없으면 0
- // 같은 숫자 2개가 페어로 나오는 경우보다 3개가 페어이면 큰값
- // 같은숫자 3개가 페어로 나오는경우보다 4개가 페어이면 큰값
- // 같은 숫자 5개 이상이면 4개가 페어인것과 같음
- // 양쪽모두페어개수가 같다면 더 큰 숫자가 큰 값
- // 숫자페어가 2벌이 나오면 더 큰 숫자로 되어있는 페어만
- // 양쪽 배열에서 페어 숫자가 같으면 비교를 할 수 없으니 0
- function check(arr1, arr2){
- let count1 = {};
- let count2 = {};
- let pair1;
- let pair2;
- for(let i = 0; i < arr1.length; i++){
- if(String(arr1[i]) in count1) count1[arr1[i]]++;
- else count1[arr1[i]] = 1;
- if(String(arr2[i]) in count2) count2[arr2[i]]++;
- else count2[arr2[i]] = 1;
- }
- Object.keys(count1).forEach(function (key){
- if(count1[key]===1) delete count1[key];
- })
- Object.keys(count2).forEach(function (key){
- if(count2[key]===1) delete count2[key];
- })
- for(let pairNum in count1){
- if(pair1===undefined) pair1 = [pairNum, count1[pairNum]];
- else if(pair1[1] < count1[pairNum] && pair1[1]<4) pair1 = [pairNum, count1[pairNum]];
- else if(pair1[0] < pairNum && pair1[1] === count1[pairNum]) pair1 = [pairNum, count1[pairNum]]
- }
- for(let pairNum in count2){
- if(pair2===undefined) pair2 = [pairNum, count2[pairNum]];
- else if(pair2[1] < count2[pairNum] && pair2[1]<4) pair2 = [pairNum, count2[pairNum]];
- else if(pair2[0] < pairNum && pair2[1] === count2[pairNum]) pair2 = [pairNum, count2[pairNum]]
- }
- if(pair1 === undefined || pair2 === undefined){
- if(pair1 === pair2) return 0;
- else if(pair1 === undefined) return 2;
- else if(pair2 === undefined) return 1;
- }
- else if(pair1[1] === pair2[1] || (pair1[1]>=4 && pair2[1]>=4)) {
- if(pair1[0] > pair2[0]) return 1;
- else if(pair1[0]< pair2[0]) return 2;
- else return 0;
- }
- else return pair1[1] > pair2[1] ? 1 : 2;
- }
- function isEmpty(obj){
- if(Object.keys(obj).length===0) return true;
- else return false;
- }
- check([1,1,1,1,1,1,1], [2,2,2,2,1,1,3]) === 2
- check([1,5,7,2,9,13,10], [2,3,9,10,4,8,11]) === 0
- check([1,4,1,3,5,6,10], [9,2,3,1,3,4,10]) === 2
- check([1,1,9,4,1,3,11],[2,3,3,13,12,9,9]) === 1
- check([1,4,9,4,1,10,13], [11,13,9,3,1,9,1]) === 2
- check([1,4,4,4,1,10,4], [11,13,11,3,11,9,1]) === 1
- check([1,2,2,3,2,2,2], [4,5,4,5,4,5,4]) === 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement