jayati

Contains Duplicate II

Feb 20th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool containsNearbyDuplicate(vector<int>& nums, int k) {
  4.         int n = nums.size();
  5.         unordered_map<int,int> m;
  6.         for(int i=0; i<n; i++)
  7.         {
  8.             if(m.count(nums[i]))
  9.             {
  10.                 if(abs(i-m[nums[i]])<=k)
  11.                     return true;
  12.             }
  13.             m[nums[i]] = i;
  14.         }
  15.      
  16.         return false;
  17.        
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment