JavaScript Code Evaluation Questions – Arrays & String Logic
Practice essential JavaScript coding interview questions focused on arrays and string manipulation. This curated set helps you master problem-solving patterns like reversing, filtering, sorting, and transforming data — a must for frontend and fullstack developer interviews.
🔄 Array & String Logic
- 1. Remove Duplicates from Array
Use Set to eliminate duplicate values as it stores only unique entries.
const arr = [1, 2, 2, 3, 4, 4, 5]; const unique = [...new Set(arr)]; console.log(unique); // [1, 2, 3, 4, 5]
Use filter() and indexOf() to keep the first occurrence of each item.
const arr = [1, 2, 2, 3, 4, 4, 5]; const unique = arr.filter((value, index, self) => self.indexOf(value) === index); console.log(unique); // [1, 2, 3, 4, 5]
Use reduce() to build a unique array manually.
const arr = [1, 2, 2, 3, 4, 4, 5]; const unique = arr.reduce((acc, curr) => { if (!acc.includes(curr)) acc.push(curr); return acc; }, []); console.log(unique); // [1, 2, 3, 4, 5]
Use a for loop and check if value already exists in the result array.
const arr = [1, 2, 2, 3, 4, 4, 5]; const unique = []; for (let i = 0; i < arr.length; i++) { if (!unique.includes(arr[i])) unique.push(arr[i]); } console.log(unique); // [1, 2, 3, 4, 5]
- 2. Reverse a String
Use split, reverse, and join to reverse characters.
const str = "hello"; const reversed = str.split('').reverse().join(''); console.log(reversed); // "olleh"
Use a for loop to reverse manually.
const str = "hello"; let reversed = ""; for (let i = str.length - 1; i >= 0; i--) { reversed += str[i]; } console.log(reversed); // "olleh"
Use reduce() for a functional approach to reverse.
const str = "hello"; const reversed = str.split('').reduce((rev, char) => char + rev, ''); console.log(reversed); // "olleh"
- 3. Check Palindrome
Check if a string reads the same forward and backward.
function isPalindrome(s) { return s === s.split('').reverse().join(''); } console.log(isPalindrome("racecar")); // true
- 4. Find Maximum and Minimum in Array
Use Math.max() and Math.min() with spread syntax.
const arr = [1, 3, 2, 5]; console.log(Math.max(...arr)); // 5 console.log(Math.min(...arr)); // 1
- 5. Flatten Nested Array
Use flat(Infinity) to recursively flatten all nested arrays.
const arr = [1, [2, [3, [4]]]]; console.log(arr.flat(Infinity)); // [1, 2, 3, 4]
- 6. Check Anagram
Anagrams are strings with same characters in different order.
const isAnagram = (a, b) => a.split('').sort().join('') === b.split('').sort().join(''); console.log(isAnagram("listen", "silent")); // true
- 7. Find Missing Number in Sequence
Use the formula sum of 1 to n to find the missing number.
const missing = (arr) => { let n = arr.length + 1; let expected = (n * (n + 1)) / 2; let actual = arr.reduce((a, b) => a + b, 0); return expected - actual; }; console.log(missing([1, 2, 4, 5])); // 3
- 8. Count Occurrence of Each Character
Loop through each character and count how many times it appears.
const str = "hello"; const count = {}; for (let char of str) { count[char] = (count[char] || 0) + 1; } console.log(count); // { h: 1, e: 1, l: 2, o: 1 }
- 9. Find First Non-Repeating Character
Find the first character whose first and last index are same.
const str = "swiss"; const result = str.split('').find(c => str.indexOf(c) === str.lastIndexOf(c)); console.log(result); // "w"
- 10. Sum of All Elements
Use reduce() to accumulate the sum of array elements.
const arr = [1, 2, 3, 4]; const sum = arr.reduce((a, b) => a + b, 0); console.log(sum); // 10