Skip to content

Instantly share code, notes, and snippets.

@suhailroushan13
Created November 29, 2025 21:09
Show Gist options
  • Select an option

  • Save suhailroushan13/4760da98d60bdc566bf7ce68d8084714 to your computer and use it in GitHub Desktop.

Select an option

Save suhailroushan13/4760da98d60bdc566bf7ce68d8084714 to your computer and use it in GitHub Desktop.

📘 50 Basic JavaScript DSA Questions — Logic Based

(With Input → Output examples)

1️⃣ Reverse a string

Input: "hello"
Output: "olleh"

Example

function reverseString(str) {
  return str.split("").reverse().join("");
}

// Example usage:
let output = reverseString("hello")
console.log(output); // Output: "olleh"

2️⃣ Reverse the words in a sentence

Input: "I love coding"
Output: "coding love I"

3️⃣ Check if a string is a palindrome

Input: "madam"
Output: true

4️⃣ Count vowels & consonants in a string

Input: "hello"
Output: { vowels: 2, consonants: 3 }

5️⃣ Check if two strings are anagrams

Input: "listen", "silent"
Output: true

6️⃣ Find the largest element in an array

Input: [4, 1, 9, 3]
Output: 9

Example

function findLargest(arr) {
  // Logic
}
let myArr = [4, 1, 9, 3]
let output = findLargest(myArr)
console.log(output); // Output: 9

7️⃣ Find the smallest element in an array

Input: [4, 1, 9, 3]
Output: 1

8️⃣ Find second largest number

Input: [10, 20, 4, 45, 99]
Output: 45

9️⃣ Remove duplicates (no Set)

Input: [1, 2, 2, 3, 3, 4]
Output: [1, 2, 3, 4]

🔟 Count occurrences of elements

Input: [1, 2, 2, 3]
Output: {1:1, 2:2, 3:1}

11️⃣ Check if array is sorted

Input: [1, 2, 3, 4]
Output: true

12️⃣ Add all numbers in array

Input: [1, 2, 3]
Output: 6

13️⃣ Sum of even & odd numbers separately

Input: [1, 2, 3, 4]
Output: {even: 6, odd: 4}

14️⃣ Find average of array

Input: [2, 4, 6]
Output: 4

15️⃣ Find missing number (1 to N)

Input: [1, 2, 4, 5]
Output: 3

16️⃣ Intersection of two arrays

Input: [1,2,3], [2,3,4]
Output: [2,3]

17️⃣ Union of two arrays

Input: [1,2,3], [3,4,5]
Output: [1,2,3,4,5]

18️⃣ Rotate array by K

Input: [1,2,3,4,5], k=2
Output: [4,5,1,2,3]

19️⃣ Move all zeros to end

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

20️⃣ Merge two sorted arrays

Input: [1,3,5], [2,4,6]
Output: [1,2,3,4,5,6]

21️⃣ Find longest word

Input: "I love programming"
Output: "programming"

22️⃣ Remove white spaces

Input: "a b c d"
Output: "abcd"

23️⃣ Convert string to Title Case

Input: "hello world"
Output: "Hello World"

24️⃣ First non-repeating character

Input: "aabbcd"
Output: "c"

25️⃣ Print duplicate characters

Input: "aabbc"
Output: ["a","b"]

26️⃣ Factorial of a number

Input: 5
Output: 120

27️⃣ Fibonacci series up to N

Input: 6
Output: [0,1,1,2,3,5]

28️⃣ Check prime

Input: 7
Output: true

29️⃣ Print all primes in a range

Input: 1 to 10
Output: [2,3,5,7]

30️⃣ Check Armstrong number

Input: 153
Output: true

31️⃣ Find GCD & LCM

Input: 6, 8
Output: {GCD: 2, LCM: 24}

32️⃣ Decimal → Binary

Input: 10
Output: "1010"

33️⃣ Binary → Decimal

Input: "1010"
Output: 10

34️⃣ Count digits in number

Input: 12345
Output: 5

35️⃣ Reverse digits

Input: 1234
Output: 4321

36️⃣ Number palindrome

Input: 121
Output: true

37️⃣ Digit frequency

Input: 112233
Output: {1:2, 2:2, 3:2}

38️⃣ Print 1–100 skipping multiples of 3

Output: 1,2,4,5,7,8,...

39️⃣ FizzBuzz

Output: Multiples of 3 → Fizz, of 5 → Buzz, both → FizzBuzz

40️⃣ Random number in range

Input: min=1, max=10
Output: A number between 1–10

41️⃣ Sort array manually

Input: [3,1,2]
Output: [1,2,3]

42️⃣ Find min/max without Math.min/max

Input: [10,4,6]
Output: min=4, max=10

43️⃣ Remove specific element

Input: [1,2,3,2], target=2
Output: [1,3]

44️⃣ All pairs with given sum

Input: [1,2,3,4], target=5
Output: [(1,4),(2,3)]

45️⃣ Check if sum of any two exists

Input: [1,2,3], target=6
Output: false

46️⃣ Flatten nested array

Input: [1, [2, 3], [4]]
Output: [1,2,3,4]

47️⃣ Chunk array into groups

Input: [1,2,3,4,5], size=2
Output: [[1,2],[3,4],[5]]

48️⃣ Count positive, negative & zero

Input: [1, -2, 0, 3]
Output: {positive:2, negative:1, zero:1}

49️⃣ Replace negative values with 0

Input: [1,-2,3,-4]
Output: [1,0,3,0]

5️⃣0️⃣ Swap two variables (no third variable)

Input: a=5, b=10
Output: a=10, b=5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment