-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2677-ChunkArray.js
More file actions
62 lines (55 loc) · 1.91 KB
/
2677-ChunkArray.js
File metadata and controls
62 lines (55 loc) · 1.91 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 2677. Chunk Array
// Given an array arr and a chunk size size, return a chunked array.
// A chunked array contains the original elements in arr, but consists of subarrays each of length size.
// The length of the last subarray may be less than size if arr.length is not evenly divisible by size.
// You may assume the array is the output of JSON.parse. In other words, it is valid JSON.
// Please solve it without using lodash's _.chunk function.
// Example 1:
// Input: arr = [1,2,3,4,5], size = 1
// Output: [[1],[2],[3],[4],[5]]
// Explanation: The arr has been split into subarrays each with 1 element.
// Example 2:
// Input: arr = [1,9,6,3,2], size = 3
// Output: [[1,9,6],[3,2]]
// Explanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.
// Example 3:
// Input: arr = [8,5,3,2,6], size = 6
// Output: [[8,5,3,2,6]]
// Explanation: Size is greater than arr.length thus all elements are in the first subarray.
// Example 4:
// Input: arr = [], size = 1
// Output: []
// Explanation: There are no elements to be chunked so an empty array is returned.
// Constraints:
// arr is a valid JSON array
// 2 <= JSON.stringify(arr).length <= 10^5
// 1 <= size <= arr.length + 1
/**
* @param {Array} arr
* @param {number} size
* @return {Array}
*/
var chunk = function(arr, size) {
if(arr.length == 0) return [];
if (size >= arr.length) return [arr];
let res = [];
for ( let i = 0; i < arr.length; i = i + size) {
let temp = [];
let j = 0;
while(j < size && i + j < arr.length) {
temp.push(arr[i + j])
j++;
}
res.push(temp);
}
return res;
};
// use arr.slice
var chunk = function (arr, size) {
let num = Math.ceil(arr.length/size)
let newArr = []
for(let i=0;i<num;i++){
newArr.push(arr.slice(i*size,(i+1)*size))
}
return newArr
};