LeetCode: Week 1-Counting Elements
30-day leet coding challenge
Problem Statement :
Table of Contents:
Code Solution:
Time Complexity: O(n) | Space Complexity: O(n)
Explanation:
For this problem, we’ll be using Hashmap to store all the values present in the given array. We’ll be using it to identify whether x + 1
present in the array or not?
let’s start with the first for
loop — To store all value in hashMap
After completion of for loop, we’ll have
hashMap ={'0':true, '1':true, ‘2’:true, ‘3’:true, ‘5’:true}
now, In the second for
loop, we’ll be checking for each x + 1
value present in hashMap then Increment count
.
After execution second for
loop, we get count = 3
as the expected result.
Run Code:
You can also check out other problem solution below
I hope you enjoy this solution. Let me know if you have any other solutions in response. Cheers :)