Javascript Array Coding Challenges
3 min readJan 20, 2026
Js Array Methods
1. Remove duplicate elements
const arr = [1, 2, 2, 3, 4, 4];
const unique = [...new Set(arr)];
console.log(unique); // Output: [1, 2, 3, 4]const arr = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = arr.filter((element, index) => {
return arr.indexOf(element) === index
})
console.log(uniqueArray) // [1, 2, 3, 4, 5]2. Find sum of array elements
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 153. Find second largest number
const arr = [10, 20, 5, 8, 20, 10];
const sorted = [...new Set(arr)].sort((a, b) => b - a);
console.log(sorted[1]); // Output: 104. Check if array is sorted
const isSorted = arr.every((v, i, a) => i === 0 || a[i - 1] <= v);
console.log(isSorted); // Output: true if sorted in ascending order, false otherwise5. Find intersection of two arrays
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 2, 6, 3, 8];
const intersection = arr1.filter(x => arr2.includes(x));
console.log(intersection); // Output: [2, 3, 4]6. Find union of two arrays
const arr1 = [1, 2, 3, 4, 5]
const arr2 = [4, 2, 6, 3, 8]
const union = [...new Set([...arr1, ...arr2])]
console.log(union) // Output: [1, 2, 3, 4, 5, 6, 8]7. Remove falsy values
const arr = [0, false, "", null, 5];
const truthy = arr.filter(Boolean);
console.log(truthy);// Output: [5]8. Count occurrences of elements
const arr = ["a", "b", "a"];
const count = arr.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});
console.log(count); // Output: { a: 2, b: 1 }Get Tarun Pandey’s stories in your inbox
Join Medium for free to get updates from this writer.
9. Flatten deeply nested array
const nestedArr = [1, [2, [3, 4]], 5];
console.log(nestedArr.flat(Infinity)); // Output: [1, 2, 3, 4, 5]10. Find missing number
// Solution 01
const arr = [1, 2, 4, 5];
const n = 5;
const missing = (n * (n + 1)) / 2 - arr.reduce((a, b) => a + b, 0);
console.log(missing); // Output: 3
// Solution 02
const arr = [1, 2, 4, 5];
const n = arr.length + 1;
const sum = (n * (n + 1)) / 2;
const missing = sum - arr.reduce((a,b)=>a+b,0);
console.log(missing); // Output: 311. Chunk array
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const chunk = (arr, size) => {
const res = [];
for (let i = 0; i < arr.length; i += size) {
console.log(i);
res.push(arr.slice(i, i + size));
}
return res;
};
chunk(arr,2); // Output: [[1,2],[3,4],[5,6],[7,8],[9]]12. Find duplicate elements
let arr = [1, 2, 3, 2, 5, 3, 7, 8, 9];
const duplicates = arr.filter((v, i, a) => a.indexOf(v) !== i);
console.log(duplicates); // Output: [2, 3]13. Find max & min without Math
let arr = [1, 2, 3, 2, 5, 3, 7, 8, 9];
let max = arr[0], min = arr[0];
for (let num of arr) {
if (num > max) max = num;
if (num < min) min = num;
}
console.log("Max:", max); // Output: 9
console.log("Min:", min); // Output: 114. Remove specific element
let arr = [1, 2, 3, 2, 5, 3, 7, 8, 9];
const result = arr.filter(x => x !== 3);
console.log(result); // Output: [1, 2, 2, 5, 7, 8, 9]15. Check if two arrays are equal
let arrA = [1, 2, 3, 4, 5];
let arrB = [1, 2, 3, 4, 4];
const isEqual = (a, b) =>
a.length === b.length && a.every((v, i) => v === b[i]);
console.log(isEqual(arrA,arrB)); // Output: false16. Convert array to object
const arr = ["a", "b"];
const obj = arr.reduce((acc, v, i) => {
acc[i] = v;
return acc;
}, {});
console.log(obj); // { '0': 'a', '1': 'b' }17. Group elements by property
const users = [{role: "admin"}, {role: "user"}];
const grouped = users.reduce((acc, u) => {
acc[u.role] = acc[u.role] || [];
acc[u.role].push(u);
return acc;
}, {});
console.log(grouped); // Output: { admin: [ { role: 'admin' } ], user: [ { role: 'user' } ] }18. Find longest string in array
const arr = ["apple", "banana", "cherry", "date"];
const longest = arr.reduce((a, b) => a.length > b.length ? a : b);
console.log(longest); // Output: "banana"




