Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

First Non Repeating Character Using Hash in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Hash Mastery: First Non Repeating Character
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of First Non Repeating Character Index
What is the output of the following JavaScript code that finds the index of the first non-repeating character in a string?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
  const charCount = {};
  for (const ch of s) {
    charCount[ch] = (charCount[ch] || 0) + 1;
  }
  for (let i = 0; i < s.length; i++) {
    if (charCount[s[i]] === 1) return i;
  }
  return -1;
}

console.log(firstNonRepeatingCharIndex("swiss"));
A3
B2
C1
D-1
Attempts:
2 left
๐Ÿ’ก Hint
Check the count of each character and find the first with count 1.
โ“ Predict Output
intermediate
2:00remaining
Output of First Non Repeating Character Index with all repeating
What is the output of the following JavaScript code when all characters repeat?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
  const charCount = {};
  for (const ch of s) {
    charCount[ch] = (charCount[ch] || 0) + 1;
  }
  for (let i = 0; i < s.length; i++) {
    if (charCount[s[i]] === 1) return i;
  }
  return -1;
}

console.log(firstNonRepeatingCharIndex("aabbcc"));
A6
B0
C1
D-1
Attempts:
2 left
๐Ÿ’ก Hint
If no character appears once, the function returns -1.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in this First Non Repeating Character code
What error does the following code produce when run?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
  const charCount = new Map();
  for (const ch of s) {
    if (charCount.has(ch)) {
      charCount.set(ch, charCount.get(ch) + 1);
    } else {
      charCount.set(ch, 1);
    }
  }
  for (let i = 0; i < s.length; i++) {
    if (charCount.get(s[i]) === 1) return i;
  }
  return -1;
}

console.log(firstNonRepeatingCharIndex("hello"));
ANo error, outputs -1
BTypeError: charCount.set is not a function
CTypeError: charCount.get is not a function
DTypeError: charCount.has is not a function
Attempts:
2 left
๐Ÿ’ก Hint
Check how Map methods are used and how properties are assigned.
โ“ Predict Output
advanced
2:00remaining
Output of First Non Repeating Character with Unicode characters
What is the output of the following code when the input string contains Unicode characters?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
  const charCount = {};
  for (const ch of s) {
    charCount[ch] = (charCount[ch] || 0) + 1;
  }
  for (let i = 0; i < s.length; i++) {
    if (charCount[s[i]] === 1) return i;
  }
  return -1;
}

console.log(firstNonRepeatingCharIndex("aรกa"));
A-1
B1
C2
D0
Attempts:
2 left
๐Ÿ’ก Hint
Unicode characters are treated as distinct keys in the hash.
๐Ÿš€ Application
expert
2:00remaining
How many unique characters are counted in this string?
Using the hash counting method, how many unique characters does the string "abcabcdeff" have?
DSA Javascript
function countUniqueChars(s) {
  const charCount = {};
  for (const ch of s) {
    charCount[ch] = (charCount[ch] || 0) + 1;
  }
  return Object.keys(charCount).length;
}

console.log(countUniqueChars("abcabcdeff"));
A7
B8
C6
D9
Attempts:
2 left
๐Ÿ’ก Hint
Count distinct characters in the string.

Practice

(1/5)
1. What is the main purpose of using a hash (object) in finding the first non repeating character in a string?
easy
A. To reverse the string
B. To sort the characters alphabetically
C. To remove duplicate characters from the string
D. To count the frequency of each character in the string

Solution

  1. Step 1: Understand the role of hash in frequency counting

    A hash (object) stores each character as a key and counts how many times it appears in the string.
  2. Step 2: Use frequency to find first non repeating character

    By checking the hash, we find the first character with count 1, which is the first non repeating character.
  3. Final Answer:

    To count the frequency of each character in the string -> Option D
  4. Quick Check:

    Hash counts characters = Frequency count [OK]
Hint: Use hash to count characters, then find first with count 1 [OK]
Common Mistakes:
  • Trying to sort characters instead of counting
  • Ignoring character frequency
  • Using hash to reverse string
2. Which of the following is the correct syntax to initialize an empty hash (object) in JavaScript for counting characters?
easy
A. let hash = [];
B. let hash = {};
C. let hash = ();
D. let hash = '';

Solution

  1. Step 1: Identify JavaScript object syntax

    In JavaScript, curly braces {} create an empty object, which is used as a hash for key-value pairs.
  2. Step 2: Confirm correct initialization

    Using let hash = {}; correctly initializes an empty object for counting characters.
  3. Final Answer:

    let hash = {}; -> Option B
  4. Quick Check:

    Empty object = {} [OK]
Hint: Use {} to create empty object for hash in JavaScript [OK]
Common Mistakes:
  • Using [] which creates an array, not an object
  • Using () which is invalid syntax
  • Using empty string '' which is not an object
3. What will be the output of the following code?
function firstNonRepeatingChar(str) {
  const hash = {};
  for (const ch of str) {
    hash[ch] = (hash[ch] || 0) + 1;
  }
  for (const ch of str) {
    if (hash[ch] === 1) return ch;
  }
  return '_';
}

console.log(firstNonRepeatingChar('swiss'));
medium
A. 'w'
B. 's'
C. 'i'
D. '_'

Solution

  1. Step 1: Count character frequencies in 'swiss'

    Characters: s(3), w(1), i(1). Hash = {s:3, w:1, i:1}
  2. Step 2: Find first character with count 1 in original order

    Order: s, w, i, s, s. First with count 1 is 'w'.
  3. Final Answer:

    'w' -> Option A
  4. Quick Check:

    First unique char in 'swiss' = 'w' [OK]
Hint: Check counts, then scan string for first count 1 char [OK]
Common Mistakes:
  • Returning 's' which repeats
  • Returning 'i' which is second unique
  • Returning '_' which means no unique char
4. Identify the error in the following code snippet that tries to find the first non repeating character:
function firstNonRepeatingChar(str) {
  const hash = {};
  for (let i = 0; i <= str.length; i++) {
    const ch = str[i];
    hash[ch] = (hash[ch] || 0) + 1;
  }
  for (let i = 0; i < str.length; i++) {
    if (hash[str[i]] === 1) return str[i];
  }
  return '_';
}
medium
A. The hash is not initialized correctly
B. The second loop should use <= instead of <
C. The first loop uses <= instead of < causing undefined access
D. The return '_' is unreachable code

Solution

  1. Step 1: Check loop conditions for index errors

    The first loop uses i <= str.length, which accesses str[str.length] (undefined), causing an error.
  2. Step 2: Correct loop condition to i < str.length

    Changing to i < str.length prevents out-of-bounds access and correctly counts characters.
  3. Final Answer:

    The first loop uses <= instead of < causing undefined access -> Option C
  4. Quick Check:

    Loop index must be less than length [OK]
Hint: Use < not <= for string index loops to avoid undefined [OK]
Common Mistakes:
  • Using <= causes out-of-range errors
  • Ignoring loop boundary conditions
  • Misunderstanding hash initialization
5. Given the string 'aabbccddeeffg', which modification to the code below will correctly return the first non repeating character?
function firstNonRepeatingChar(str) {
  const hash = {};
  for (const ch of str) {
    hash[ch] = (hash[ch] || 0) + 1;
  }
  for (const ch of str) {
    if (hash[ch] === 1) return ch;
  }
  return '_';
}

Options:
A) Change return '_' to return null
B) Add a check to ignore spaces in the string
C) Modify the function to return '_' if no unique character exists
D) Change the hash to store boolean instead of counts
hard
A. Change return '_' to return null
B. Add a check to ignore spaces in the string
C. Modify the function to return '_' if no unique character exists
D. Change the hash to store boolean instead of counts

Solution

  1. Step 1: Analyze the string and current function behavior

    String 'aabbccddeeffg' has all characters repeated except 'g' which appears once.
  2. Step 2: Confirm function returns '_' if no unique char

    The function already returns '_' if no unique character is found, which is correct behavior.
  3. Step 3: Understand why changing return '_' to return null is better

    Changing return '_' to return null clearly indicates no unique character found, which is a better practice than returning a character that might be confused as valid.
  4. Final Answer:

    Change return '_' to return null -> Option A
  5. Quick Check:

    Return null when no unique char found is clearer [OK]
Hint: Return null to indicate no unique character found [OK]
Common Mistakes:
  • Returning '_' which can be confused as a valid character
  • Ignoring spaces when not needed
  • Using boolean instead of counts loses frequency info
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard