public int firstUniqChar(String s) {
HashMap<Character, Integer> charCount = new HashMap<>();
// Count the occurrences of each character in the string
for (char val : s.toCharArray()) {
charCount.put(val, charCount.getOrDefault(val, 0) + 1);
}
// Find the index of the first non-repeating character
for (int i = 0; i < s.length(); i++) {
if (charCount.get(s.charAt(i)) == 1) {
return i;
}
}
// If no unique character is found, return -1
return -1;
}In this code i need to understand the code for this line charCount.put(val, charCount.getOrDefault(val, 0) + 1);
I just need to undersatnd what getOrDefault() method do. Can someone explain this in detail with a an example code.
Thanks in advance!!