Bird
Raised Fist0
DSA Javaprogramming~20 mins

First Non Repeating Character Using Hash in DSA Java - 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 finding first non-repeating character index
What is the output of the following Java code that finds the index of the first non-repeating character in the string "swiss"?
DSA Java
import java.util.*;
public class Main {
    public static int firstNonRepeatingChar(String s) {
        Map<Character, Integer> countMap = new HashMap<>();
        for (char c : s.toCharArray()) {
            countMap.put(c, countMap.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (countMap.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        System.out.println(firstNonRepeatingChar("swiss"));
    }
}
A1
B0
C2
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 for "aabbcc"
What is the output of the following Java code that finds the index of the first non-repeating character in the string "aabbcc"?
DSA Java
import java.util.*;
public class Main {
    public static int firstNonRepeatingChar(String s) {
        Map<Character, Integer> countMap = new HashMap<>();
        for (char c : s.toCharArray()) {
            countMap.put(c, countMap.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (countMap.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        System.out.println(firstNonRepeatingChar("aabbcc"));
    }
}
A5
B0
C2
D-1
Attempts:
2 left
💡 Hint
Check if any character appears only once.
🔧 Debug
advanced
2:00remaining
Identify the error in the code for first non-repeating character
What error will the following Java code produce when run?
DSA Java
import java.util.*;
public class Main {
    public static int firstNonRepeatingChar(String s) {
        Map<Character, Integer> countMap = new HashMap<>();
        for (int i = 0; i <= s.length(); i++) {
            char c = s.charAt(i);
            countMap.put(c, countMap.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (countMap.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        System.out.println(firstNonRepeatingChar("hello"));
    }
}
ANo error, output is 0
BNullPointerException
CStringIndexOutOfBoundsException
DCompilation error
Attempts:
2 left
💡 Hint
Check the loop boundary in the first for loop.
🧠 Conceptual
advanced
2:00remaining
Why use a hash map for first non-repeating character?
Why is a hash map (or dictionary) used to find the first non-repeating character in a string efficiently?
AIt stores the count of each character for quick lookup in O(1) time.
BIt sorts the characters alphabetically to find the first unique one.
CIt removes duplicate characters from the string.
DIt reverses the string to check characters from the end.
Attempts:
2 left
💡 Hint
Think about how to count characters quickly and check their counts.
❓ Predict Output
expert
2:00remaining
Output of first non-repeating character index with Unicode characters
What is the output of the following Java code when the input string contains Unicode characters "aáa"?
DSA Java
import java.util.*;
public class Main {
    public static int firstNonRepeatingChar(String s) {
        Map<Character, Integer> countMap = new HashMap<>();
        for (char c : s.toCharArray()) {
            countMap.put(c, countMap.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (countMap.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        System.out.println(firstNonRepeatingChar("aáa"));
    }
}
A0
B1
C2
D-1
Attempts:
2 left
💡 Hint
Unicode characters are treated as distinct chars.

Practice

(1/5)
1. What does the "First Non Repeating Character Using Hash" algorithm primarily do?
easy
A. Reverses the string using a hash map
B. Counts the total number of characters in a string
C. Sorts characters of a string in alphabetical order
D. Finds the first character in a string that appears only once

Solution

  1. Step 1: Understand the problem goal

    The algorithm aims to find the first character in the string that does not repeat anywhere else.
  2. Step 2: Role of hash map

    A hash map stores character counts to quickly check which characters appear once.
  3. Final Answer:

    Finds the first character in a string that appears only once -> Option D
  4. Quick Check:

    First non repeating character = Finds the first character in a string that appears only once [OK]
Hint: Look for the first unique character using counts [OK]
Common Mistakes:
  • Thinking it counts total characters only
  • Confusing sorting with finding unique character
  • Assuming it reverses the string
2. Which Java collection is best suited to store character counts for this problem?
easy
A. ArrayList<Character>
B. HashMap
C. LinkedList<Character>
D. TreeSet<Character>

Solution

  1. Step 1: Identify data structure needs

    We need to map each character to its count, so a key-value structure is required.
  2. Step 2: Choose appropriate Java collection

    HashMap stores characters as keys and their counts as values efficiently.
  3. Final Answer:

    HashMap -> Option B
  4. Quick Check:

    Use HashMap for character counts [OK]
Hint: Use HashMap for key-value pairs of char and count [OK]
Common Mistakes:
  • Using list which doesn't map keys to counts
  • Using TreeSet which stores unique sorted elements only
  • Using LinkedList which is not key-value based
3. What is the output of the following Java code snippet?
String s = "swiss";
HashMap map = new HashMap<>();
for(char c : s.toCharArray()) {
  map.put(c, map.getOrDefault(c, 0) + 1);
}
for(char c : s.toCharArray()) {
  if(map.get(c) == 1) {
    System.out.println(c);
    break;
  }
}
medium
A. w
B. s
C. i
D. No output

Solution

  1. Step 1: Count character frequencies

    Characters and counts: s=3, w=1, i=1.
  2. Step 2: Find first character with count 1

    Iterate string: 's'(3), 'w'(1) -> first non repeating is 'w'.
  3. Final Answer:

    w -> Option A
  4. Quick Check:

    First unique char in "swiss" = w [OK]
Hint: Check counts then first unique char in order [OK]
Common Mistakes:
  • Choosing 's' which repeats
  • Choosing 'i' which is unique but appears after 'w'
  • Assuming no output if not careful
4. Identify the error in this code snippet for finding the first non repeating character:
String s = "hello";
HashMap map = new HashMap<>();
for(int i = 0; i < s.length(); i++) {
  map.put(s.charAt(i), 1);
}
for(int i = 0; i < s.length(); i++) {
  if(map.get(s.charAt(i)) == 1) {
    System.out.println(s.charAt(i));
    break;
  }
}
medium
A. The map.put always sets count to 1, ignoring repeats
B. The loop should start from 1, not 0
C. The map should use Character keys, not int
D. The break statement is missing

Solution

  1. Step 1: Analyze map population logic

    The code sets count to 1 for every character without incrementing, so repeats are not counted.
  2. Step 2: Effect on finding non repeating character

    Since all counts are 1, the code incorrectly treats all characters as unique.
  3. Final Answer:

    The map.put always sets count to 1, ignoring repeats -> Option A
  4. Quick Check:

    Count update missing causes wrong results [OK]
Hint: Increment count instead of setting 1 every time [OK]
Common Mistakes:
  • Not incrementing counts in map
  • Wrong loop indices
  • Confusing key types in map
5. Given the string "aabbccddeeffg", which character will be the first non repeating character using the hash method? What if the string is "aabbccddeeffggh"?
hard
A. First: g, Second: g
B. First: a, Second: h
C. First: g, Second: h
D. First: h, Second: h

Solution

  1. Step 1: Analyze first string "aabbccddeeffg"

    All characters except 'g' appear twice; 'g' appears once, so first non repeating is 'g'.
  2. Step 2: Analyze second string "aabbccddeeffggh"

    Now 'g' appears twice, 'h' appears once, so first non repeating is 'h'.
  3. Final Answer:

    First: g, Second: h -> Option C
  4. Quick Check:

    Check counts carefully for each string [OK]
Hint: Check counts carefully; last unique changes with input [OK]
Common Mistakes:
  • Assuming first character is always unique
  • Ignoring updated counts in second string
  • Confusing repeated characters as unique
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