일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 개발자_조이킴
- 프로그래머스
- 코드스테이츠
- 최강의 인생
- 역행자
- array
- 코플릿
- JavaScript
- join
- 코딩공부
- 정규표현식
- 재귀함수
- 코딩테스트
- Algorithms
- array.slice()
- Hackerrank
- SQL
- Developer_JoyKim
- 알고리즘
- array.push()
- select
- 자바스크립트
- node.js
- Programmers
- Where
- MySQL
- 블록체인
- for문
- 배열
- 개발자의 책장
Archives
- Today
- Total
CodingSpace
[HackerRank/Algorithms] Implementation - Equalize the Array 본문
HackerRank/Algorithm
[HackerRank/Algorithms] Implementation - Equalize the Array
개발자_조이킴 2022. 9. 8. 23:50Problem. Implementation - Equalize the Array
Link.
https://www.hackerrank.com/challenges/equality-in-a-array/problem?isFullScreen=true
Description.
Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.
Key Point.
My Answer.
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'equalizeArray' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
function equalizeArray(arr) {
// 배열 arr의 전체 길이
let totalLen = arr.length;
let obj = {};
// 각 요소별 중복되는 개수를 키-값으로 저장
arr.forEach((el) => {
obj[el] = (obj[el] || 0) + 1;
})
// 각 키의 값들을 배열로 저장
let array = Object.values(obj);
// 중복된 값 중 가장 큰 값을 변수 max에 저장
let max = Math.max(...array);
// 전체 길이에서 max를 뺀 값을 반환
return totalLen - max;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine().trim(), 10);
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
const result = equalizeArray(arr);
ws.write(result + '\n');
ws.end();
}
References.
'HackerRank > Algorithm' 카테고리의 다른 글
[HackerRank/Algorithms] Search - Ice Cream Parlor (0) | 2022.09.11 |
---|---|
[HackerRank/Algorithms] Sorting - Big Sorting (0) | 2022.09.10 |
[HackerRank/Algorithms] Strings - CamelCase (0) | 2022.09.06 |
[HackerRank/Algorithms] Implementation - Cut the sticks (0) | 2022.09.04 |
[HackerRank/Algorithms] Warmup - Compare the Triplets (0) | 2022.09.03 |
Comments