Skip to content

Commit 6547af2

Browse files
committedJan 7, 2025
Add solution #2677
1 parent 956cb72 commit 6547af2

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@
398398
2665|[Counter II](./2665-counter-ii.js)|Easy|
399399
2666|[Allow One Function Call](./2666-allow-one-function-call.js)|Easy|
400400
2667|[Create Hello World Function](./2667-create-hello-world-function.js)|Easy|
401+
2677|[Chunk Array](./2677-chunk-array.js)|Easy|
401402
2703|[Return Length of Arguments Passed](./2703-return-length-of-arguments-passed.js)|Easy|
402403
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
403404
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|

‎solutions/2677-chunk-array.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 2677. Chunk Array
3+
* https://leetcode.com/problems/chunk-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an array arr and a chunk size size, return a chunked array.
7+
*
8+
* A chunked array contains the original elements in arr, but consists of subarrays each of
9+
* length size. The length of the last subarray may be less than size if arr.length is not
10+
* evenly divisible by size.
11+
*
12+
* You may assume the array is the output of JSON.parse. In other words, it is valid JSON.
13+
*
14+
* Please solve it without using lodash's _.chunk function.
15+
*/
16+
17+
/**
18+
* @param {Array} arr
19+
* @param {number} size
20+
* @return {Array}
21+
*/
22+
var chunk = function(arr, size) {
23+
var result = [];
24+
25+
for (let index = 0; index < arr.length;) {
26+
result.push(arr.slice(index, index + size));
27+
index += size;
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)
Please sign in to comment.