# Ngày 28/05 ### Bài số 1 Input: [1,2,4], [1,3,4,5] Output: [1,1,2,3,4,4,5] ```typescript= function mergeArr(arr1: number[], arr2: number[]): number[] { return [...arr1, ...arr2].sort((a,b)=> a-b); } ``` ### Bài số 2 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] ```typescript= function reOrderArr(arr: number[], k: number): number[] { return [...arr.splice(arr.length-k), ...arr]; } ``` *- Hàm **splice** cắt mảng và loại bỏ phần tử đã cắt ra khỏi mảng cũ* *- Hàm **slice** cắt mảng và vẫn giữ nguyên các phẩn từ ở mảng cũ* ### Bài số 3 Input: nums = [1,2,3,1] Output: true Input: nums = [1,2,3,4] Output: false Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true ```typescript= function checkArr(arr: number[]): boolean { return arr.some(x => arr.filter(y => y == x).length>1); } ``` ### Bài số 4 Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2: Input: nums = [1,3,5,6], target = 2 Output: 1 Example 3: Input: nums = [1,3,5,6], target = 7 Output: 4 ```typescript= function checkIndex(arr: number[], target: number): number { return target >= Math.max(...arr) ? arr.length : arr.findIndex(x => target <= x); } ``` ### Bài số 5 Example 1: Input: nums = [1,2,1,3,2,5] Output: [3,5] Example 2: Input: nums = [-1,0] Output: [-1,0] Example 3: Input: nums = [0,1] Output: [1,0] ```typescript= function setArr(arr: number[]): number[] { return arr.filter(x => arr.filter(y => y==x).length == 1); } ``` ### Bài số 6 Example 1: Input: num = 38 Output: 2 Explanation: The process is *38 --> 3 + 8 --> 11* *11 --> 1 + 1 --> 2* Since 2 has only one digit, return it. Example 2: Input: num = 0 Output: 0 ```typescript= function processNum(num: number): number { while((num + '').length > 1) { num = (num + '').split('').map(x => +x).reduce((x, y) => x + y); } return num; } ```