Back to Exercises

Arrays (Real Patterns)

20 Exercises Available

map/filter/reduce + real-world data handling patterns.

1.Easy

Remove Duplicates (keep order)

Create a function `unique(arr)` that removes duplicates but preserves original order.

Sample Output:

unique([1, 2, 2, 3, 1, 4]) => [1, 2, 3, 4]
unique(["a", "b", "a", "c"]) => ["a", "b", "c"]
View Solution
2.Medium

Group By (category)

Given products: `{id, category}`, function `groupByCategory(items)` banao jo category wise grouped object return kare.

Sample Output:

groupByCategory([{id:1,category:"laptop"},{id:2,category:"mobile"},{id:3,category:"laptop"}])
=> {laptop: [{id:1,category:"laptop"},{id:3,category:"laptop"}], mobile: [{id:2,category:"mobile"}]}
View Solution
3.Easy

Sort by Price (ascending) without mutating original

Function `sortByPrice(items)` banao jo new sorted array return kare. Original array mutate nahi hona chahiye.

Sample Output:

sortByPrice([{price:100},{price:50},{price:200}])
=> [{price:50},{price:100},{price:200}]
View Solution
4.Medium

Top N (highest scores)

Create a function `topN(scores, n)` that returns top n numbers.

Sample Output:

topN([85, 92, 78, 96, 88], 3) => [96, 92, 88]
topN([10, 5, 20, 15], 2) => [20, 15]
View Solution
5.Easy

Flatten 1 Level

Create a function `flatten1(arr)` that flattens one-level nested arrays.

Sample Output:

flatten1([1, [2, 3], [4, 5]]) => [1, 2, 3, 4, 5]
flatten1([[1, 2], [3, 4]]) => [1, 2, 3, 4]
View Solution
6.Medium

Chunk Array into Groups

Function `chunk(arr, size)` banao jo array ko specified size ke chunks me divide kare. Example: chunk([1, 2, 3, 4, 5], 2) → [[1, 2], [3, 4], [5]]

Sample Output:

chunk([1, 2, 3, 4, 5], 2) => [[1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4], 2) => [[1, 2], [3, 4]]
View Solution
7.Medium

Find Intersection of Multiple Arrays

Create a function `intersection(...arrays)` that returns common elements in all arrays. Example: intersection([1, 2, 3], [2, 3, 4], [3, 4, 5]) → [3]

Sample Output:

intersection([1, 2, 3], [2, 3, 4], [3, 4, 5]) => [3]
intersection([1, 2], [2, 3], [2, 4]) => [2]
View Solution
8.Easy

Remove Falsy Values from Array

Create a function `compact(arr)` that removes falsy values (false, null, 0, "", undefined, NaN) from array. Example: compact([0, 1, false, 2, "", 3]) → [1, 2, 3]

Sample Output:

compact([0, 1, false, 2, "", 3]) => [1, 2, 3]
compact([null, undefined, 0, "", NaN, 1]) => [1]
View Solution
9.Easy

Find Difference Between Two Arrays

Create a function `difference(arr1, arr2)` that returns elements from arr1 that are not in arr2. Example: difference([1, 2, 3], [2, 3, 4]) → [1]

Sample Output:

difference([1, 2, 3], [2, 3, 4]) => [1]
difference([1, 2, 3], [1]) => [2, 3]
View Solution
10.Easy

Find Union of Multiple Arrays

Function `union(...arrays)` banao jo sabhi arrays ke unique elements return kare. Example: union([1, 2], [2, 3], [3, 4]) → [1, 2, 3, 4]

Sample Output:

union([1, 2], [2, 3], [3, 4]) => [1, 2, 3, 4]
union([1, 1, 2], [2, 3]) => [1, 2, 3]
View Solution
11.Medium

Rotate Array Left/Right

Create a function `rotate(arr, n, direction="right")` that rotates array by n positions. Example: rotate([1, 2, 3, 4], 1) → [4, 1, 2, 3]

Sample Output:

rotate([1, 2, 3, 4], 1) => [4, 1, 2, 3]
rotate([1, 2, 3, 4], 1, "left") => [2, 3, 4, 1]
View Solution
12.Easy

Find All Indices of Element

Create a function `indicesOf(arr, value)` that returns all indices of value in array. Example: indicesOf([1, 2, 3, 2, 4, 2], 2) → [1, 3, 5]

Sample Output:

indicesOf([1, 2, 3, 2, 4, 2], 2) => [1, 3, 5]
indicesOf([1, 1, 1], 1) => [0, 1, 2]
View Solution
13.Medium

Shuffle Array (Fisher-Yates)

Create a function `shuffle(arr)` that randomly shuffles array. Example: shuffle([1, 2, 3, 4]) → [3, 1, 4, 2] (random)

Sample Output:

shuffle([1, 2, 3, 4]) => [3, 1, 4, 2] (random)
shuffle([1, 2, 3]) => [2, 3, 1] (random)
View Solution
14.Hard

Find Consecutive Numbers

Create a function `findConsecutive(arr, count)` that finds sequences of consecutive numbers in array. Example: findConsecutive([1, 2, 3, 5, 6, 7], 3) → [[1, 2, 3], [5, 6, 7]]

Sample Output:

findConsecutive([1, 2, 3, 5, 6, 7], 3) => [[1, 2, 3], [5, 6, 7]]
findConsecutive([1, 2, 3, 4], 2) => [[1, 2], [2, 3], [3, 4]]
View Solution
15.Easy

Partition Array by Condition

Create a function `partition(arr, fn)` that divides array into two groups based on condition. Example: partition([1, 2, 3, 4], n => n % 2 === 0) → [[2, 4], [1, 3]]

Sample Output:

partition([1, 2, 3, 4], n => n % 2 === 0) => [[2, 4], [1, 3]]
partition([1, 2, 3], n => n > 2) => [[3], [1, 2]]
View Solution
16.Hard

Find Maximum Subarray Sum (Kadane)

Create a function `maxSubarraySum(arr)` that finds maximum subarray sum in array. Example: maxSubarraySum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) → 6

Sample Output:

maxSubarraySum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) => 6
maxSubarraySum([1, 2, 3]) => 6
View Solution
17.Medium

Zip Multiple Arrays

Create a function `zip(...arrays)` that zips multiple arrays (combine by index). Example: zip([1, 2], ["a", "b"], [true, false]) → [[1, "a", true], [2, "b", false]]

Sample Output:

zip([1, 2], ["a", "b"], [true, false]) => [[1, "a", true], [2, "b", false]]
zip([1], [2, 3]) => [[1, 2], [undefined, 3]]
View Solution
18.Easy

Find Missing Numbers in Range

Create a function `missingInRange(arr, start, end)` that returns missing numbers in range. Example: missingInRange([1, 3, 5], 1, 5) → [2, 4]

Sample Output:

missingInRange([1, 3, 5], 1, 5) => [2, 4]
missingInRange([1, 2, 3], 1, 5) => [4, 5]
View Solution
19.Medium

Group Array by Key

Create a function `groupBy(arr, keyFn)` that groups array based on key function. Example: groupBy([{age: 20}, {age: 30}, {age: 20}], x => x.age) → {20: [{age: 20}, {age: 20}], 30: [{age: 30}]}

Sample Output:

groupBy([{age: 20}, {age: 30}, {age: 20}], x => x.age) => {20: [{age: 20}, {age: 20}], 30: [{age: 30}]}
groupBy(["a", "bb", "ccc"], s => s.length) => {1: ["a"], 2: ["bb"], 3: ["ccc"]}
View Solution
20.Medium

Find Array Symmetric Difference

Create a function `symmetricDifference(arr1, arr2)` that returns elements not common in both arrays. Example: symmetricDifference([1, 2, 3], [2, 3, 4]) → [1, 4]

Sample Output:

symmetricDifference([1, 2, 3], [2, 3, 4]) => [1, 4]
symmetricDifference([1, 2], [3, 4]) => [1, 2, 3, 4]
View Solution