LeetCode: Week 1-Counting Elements

Jayram Manale
2 min readApr 14, 2020

--

30-day leet coding challenge

Problem Statement :

Table of Contents:

  1. Code Solution
  2. Explanation
  3. Run Code

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 forloop To store all value in hashMap

Execution Values in Each Iteration — 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 .

Execution values of each Iteration

After execution second forloop, 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 :)

--

--