Posts

Day 15: Valid Anagram – JavaScript DSA Problem with Explanation

๐Ÿš€ Day 15 – JavaScript DSA Challenge: Valid Anagram ๐Ÿ“Œ Problem: Given two strings s and t , determine if t is an anagram of s . ๐Ÿงช Examples: Input: "anagram", "nagaram" ➡️ true Input: "rat", "car" ➡️ false ✅ Solution 1: Sort and Compare function isAnagram(s, t) { return s.split('').sort().join('') === t.split('').sort().join(''); } ✅ Solution 2: Frequency Map function isAnagramFreq(s, t) { if (s.length !== t.length) return false; const count = {}; for (let char of s) { count[char] = (count[char] || 0) + 1; } for (let char of t) { if (!count[char]) return false; count[char]--; } return true; } ๐ŸŽฏ Tips: Sorting works but is slower Hash map gives best performance for large strings ๐Ÿ”— Follow Me: LinkedIn: www.linkedin.com/in/reach-jaffer YouTube: @codewithjaffer

Day 14: First Non-Repeating Character – JavaScript DSA

๐Ÿš€ Day 14 – First Non-Repeating Character in JavaScript This string problem is frequently asked in product companies like Amazon and PayPal. ๐Ÿงช Problem: Given a string, return the index of the first non-repeating character. If no such character exists, return -1. ๐Ÿ“Œ Examples: Input: "leetcode" ➡️ Output: 0 Input: "loveleetcode" ➡️ Output: 2 Input: "aabbcc" ➡️ Output: -1 ✅ JavaScript Code: function firstUniqueChar(str) { const freq = {}; for (let char of str) { freq[char] = (freq[char] || 0) + 1; } for (let i = 0; i ✨ Alternate Solution (indexOf + lastIndexOf) function firstUniqCharBuiltIn(s) { for (let i = 0; i ๐ŸŽฏ Tips: Use a hash map to store character frequency indexOf + lastIndexOf is slower but useful for short strings ๐Ÿ”— Follow for more: LinkedIn: www.linkedin.com/in/reach-jaffer YouTube: @codewithjaffer

Day 13: Find Missing Number in an Array – JavaScript DSA Challenge

๐Ÿ”” Day 13: Find Missing Number in an Array – JavaScript DSA Welcome to #Day13 of the CodeWithJaffer DSA series! ๐Ÿ“Œ Problem: Given an array containing numbers from 0 to n with one number missing, find that missing number. ๐Ÿงช Examples: Input: [3, 0, 1] Output: 2 Input: [0, 1, 2, 4, 5] Output: 3 ✅ JavaScript Code: function findMissingNumber(arr) { const n = arr.length; const total = (n * (n + 1)) / 2; const sum = arr.reduce((acc, curr) => acc + curr, 0); return total - sum; } console.log(findMissingNumber([3, 0, 1])); // 2 console.log(findMissingNumber([0, 1, 2, 4, 5])); // 3 ๐Ÿ’ก Tips: Use math formula for sum: n(n+1)/2 Reduce method calculates actual sum Subtract to get missing number ๐ŸŽฏ Bonus: Try solving with XOR approach too – it’s commonly asked in interviews! ๐Ÿ”— Follow me for more: LinkedIn: www.linkedin.com/in/reach-jaffer YouTube: @codewithjaffer

Day 12: First Non-Repeating Character in a String – JavaScript DSA Challenge

๐Ÿ”” Day 12: First Non-Repeating Character in a String – JavaScript DSA Welcome to #Day12 of our CodeWithJaffer JavaScript DSA Challenge! In this post, we will find the first non-repeating character in a given string using simple JavaScript logic. ๐Ÿ“Œ Problem Statement: Given a string, find the first character that does not repeat. If all characters repeat, return -1 . ๐Ÿงช Example: Input: "javascript" Output: "j" Input: "aabbcc" Output: -1 ✅ JavaScript Code: function firstUniqueChar(str) { let freq = {}; for (let char of str) { freq[char] = (freq[char] || 0) + 1; } for (let char of str) { if (freq[char] === 1) return char; } return -1; } // Output Examples console.log(firstUniqueChar("javascript")); // "j" console.log(firstUniqueChar("aabbcc")); // -1 ๐Ÿ’ก Tips to Remember: Use a for...of loop to count each character

Day 10: Reverse Words in a Sentence | JavaScript DSA Challenge with Output & Tips

Day 10: Reverse Words in a Sentence – JavaScript DSA Welcome to Day 10 of our #CodeWithJaffer JavaScript DSA challenge! Today’s task is to reverse every word in a sentence but preserve the word order. ๐Ÿงช Example: Input: "Code With Jaffer" Output: "edoC htiW reffaJ" ✅ JavaScript Code (2 Methods): // One-liner using map function reverseWords(str) { return str.split(" ").map(word => word.split("").reverse().join("")).join(" "); } // For-loop approach function reverseWordsLoop(str) { let words = str.split(" "); let result = []; for (let word of words) { let reversed = word.split("").reverse().join(""); result.push(reversed); } return result.join(" "); } ๐Ÿง  Output: console.log(reverseWords("Code With Jaffer")); // "edoC htiW reffaJ" ๐Ÿ“Œ Tips: Use split(" ") for splitting sentence into words split("").r...

Day 9 – Count Vowels in a String using JavaScript | DSA Challenge with Code & Explanation

  ๐Ÿง  Day 9: Count Vowels in a String – JavaScript Interview Prep Welcome to Day 9 of the #CodeWithJaffer JavaScript DSA series! Today, we’ll solve a popular string-based interview problem: “Count the number of vowels in a given string.” ๐Ÿ” Problem Statement: Write a JavaScript function that takes a string as input and returns the count of vowels (a, e, i, o, u) present in it. ✅ Example Input & Output: // Input: "CodeWithJaffer" // Output: 5 vowels (o, e, i, a, e) ๐Ÿ’ก Method 1: Using RegEx function countVowels(str) { return (str.match(/[aeiou]/gi) || []).length; } ✅ Explanation: /[aeiou]/gi matches all vowels (both uppercase and lowercase). match() returns an array of matches, or null if none. || [] ensures no error when match returns null. .length gives the count of vowels. ๐Ÿ’ก Method 2: Using For-Loop function countVowelsLoop(str) { let count = 0; const vowels = "aeiouAEIOU"; for (let char of str) { if (vowels.i...

Day 8: Find Duplicate Number in Array – JavaScript Interview Question

 ๐Ÿ“Œ Problem: > Given an array of integers, find any duplicate number. ๐Ÿงช Example: Input: [1, 2, 3, 4, 2] Output: 2 --- ✅ Solution 1 – Using Set: function findDuplicate(arr) {   const seen = new Set();   for (let num of arr) {     if (seen.has(num)) return num;     seen.add(num);   }   return -1; } --- ✅ Solution 2 – Sort & Compare: function findDuplicate(arr) {   arr.sort();   for (let i = 1; i < arr.length; i++) {     if (arr[i] === arr[i - 1]) return arr[i];   }   return -1; } --- ๐Ÿ’ก Tips to Remember: Set is great for uniqueness checks Sorting reduces space, but increases time: O(n log n) Use return -1 for "no duplicate" fallback