Hashing | Maps | Time Complexity | Collisions | Division Rule of Hashing
shing | Maps | Time Complexity | Collisions | Division Rule of Hashing-> for each loop we can use this also print using key set value Hashing: Let’s first try to understand the importance of hashing using an example: Given an array of integers: [1, 2, 1, 3, 2] and we are given some queries: [1, 3, 4, 2, 10]. For each query, we need to find out how many times the number appears in the array. For example, if the query is 1 our answer would be 2, and if the query is 4 the answer will be 0. Similarly, the following will be the answers to the given queries: Brute Force approach: As we have learned the ‘for loop’, the first approach that should come to our mind is to use it to solve this problem. For each query, we will iterate over the array using a loop. We will count the number of times the query number appears in that array i.e. increment the counter variable if the array element at that index equals the query number. In terms of function, it will look like the...
Comments
Post a Comment