find the smallest distance in a given array. const arr = [1, 4, 2, 2, 1, 5]; function smallestDistance(arr) { const distanceArray = []; for (let i = 0; i < arr.length; i++) { if (arr.lastIndexOf(arr[i]) !== arr.indexOf(arr[i])) { let distance = arr.lastIndexOf(arr[i]) - arr.indexOf(arr[i]) //get the distance distanceArray.push(distance); //push to new array } } distanceArray.sort() //sort consol..
1. This single line of short circuit evaluation is let value = options && options.value || ''; equal to this code let value; if(options){ value = options.value; } else value = ''; 2. stick > min && target.push(stick - min); if(stick > min){ target.push(stick - min); }
Spread Operator in Javascript 1. In ES6, By using the spread operator, you can simply populate an array to another array. '=' only allows you to copy the reference not the value of the array. '=' 는 단지 주소값을 전달할 뿐 진짜 값을 전달하지 못하기때문에 spread operator를 사용해서 array를 복사할 수 있다. function threeDots(arr) { let sticks = [...arr]; } 2. Math.min() and Math.max() expect a list of arguments not an array. If you w..
간단하게는 minimum value 로 each index를 subtract할 수 있는 갯수를 구하는 문제. Solution 1 in Javascript const arr = [5, 3, 2, 2, 2, 8] function cutTheSticks(arr) { //sort the array let sorted = arr.sort(); //set index[0] to a min let min = sorted[0]; console.log(sorted.length); for (let i = 0; i min) { min = sorted[i]; //change min to next shortest min console.log(sorted.le..
주어진 array 에서 최대로 중복되는 index를 제외한 나머지 index의 수를 구하는 문제 solution 1. 중복을 허용하지 않는 hashMap 을 선언. 2. 주어진 arr를 hashMap안에 넣으면서 중복되는 값이 있으면 value의 값이 1씩 증가하게 한다. 3. maximum value의 수를 찾는다. 4. 전체 array길이에서 찾아낸 maximum value를 return한다. Solution 1 -object를 이용하는 traditional way 말고 built-in function 인 Map을 이용하는 방식. function equalizeArray(arr) { //let arrObj = {}; //initialize object which is for hashmap. //but..
- Total
- Today
- Yesterday
- easy javascript algorithm
- 알고리즘
- Javascript Algorithm
- code refactoring
- HackerRank Algorithm
- hackerrank javascript solution
- Collection Framework
- java
- 프로그래머스
- hackerrank javascript
- substring()
- string class in java
- hackerrank
- compareTo()
- rest parameter
- spring boot application
- C++
- math.max
- Object type casting
- easy algorithm
- math.abs
- HashMap
- equals()
- repeat()
- algorithm
- ... in Javascript
- hackerrank solution
- javascript
- 프로그래머스 알고리즘
- 프로그래머스 알고리즘문제
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |