Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02f1cc7

Browse files
feat: ArrayChunking
1 parent 0065648 commit 02f1cc7

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { chunk } from '.';
2+
3+
test('function chunk exists', () => {
4+
expect(typeof chunk).toEqual('function');
5+
});
6+
7+
test('chunk divides an array of 10 elements with chunk size 2', () => {
8+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
9+
const chunked = chunk(arr, 2);
10+
11+
expect(chunked).toEqual([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]);
12+
});
13+
14+
test('chunk divides an array of 3 elements with chunk size 1', () => {
15+
const arr = [1, 2, 3];
16+
const chunked = chunk(arr, 1);
17+
18+
expect(chunked).toEqual([[1], [2], [3]]);
19+
});
20+
21+
test('chunk divides an array of 5 elements with chunk size 3', () => {
22+
const arr = [1, 2, 3, 4, 5];
23+
const chunked = chunk(arr, 3);
24+
25+
expect(chunked).toEqual([[1, 2, 3], [4, 5]]);
26+
});
27+
28+
test('chunk divides an array of 13 elements with chunk size 5', () => {
29+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
30+
const chunked = chunk(arr, 5);
31+
32+
expect(chunked).toEqual([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]);
33+
});

‎src/6_arrayChunking/index.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// --- Task
2+
// Given an array and chunk size, divide the array into many subarrays
3+
// where each subarray is of length size
4+
5+
type ChunkedType = number | number[];
6+
7+
// --- Examples
8+
// chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
9+
// chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
10+
// chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
11+
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
12+
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
13+
14+
// 1# Solution
15+
export const chunk = (array: number[], size: number): ChunkedType[] => {
16+
const chunked: ChunkedType[] = [];
17+
18+
for (let element of array) {
19+
const last = chunked[chunked.length - 1];
20+
21+
if (!last || (Array.isArray(last) && last.length === size)) {
22+
chunked.push([element]);
23+
} else {
24+
if (Array.isArray(last)) {
25+
last.push(element);
26+
}
27+
}
28+
}
29+
30+
return chunked;
31+
};
32+
33+
// 2# Solution | Using Recursion
34+
export const chunk = (array: number[], size: number): ChunkedType[] => {
35+
if (array.length > size) {
36+
return [array.splice(0, size), ...chunk(array, size)];
37+
} else {
38+
return [array];
39+
}
40+
}
41+
42+
// 3# Solution | Using (While) loop
43+
export const chunk = (array: number[], size: number): ChunkedType[] => {
44+
const chunked: ChunkedType[] = [];
45+
let index = 0;
46+
47+
while (index < array.length) {
48+
chunked.push(array.slice(index, index + size));
49+
index += size;
50+
}
51+
52+
return chunked;
53+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /