Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. public class Solution
  2. {
  3. public bool ContainsNearbyDuplicate(int[] nums, int k)
  4. {
  5. // value, index
  6. var dict = new Dictionary<int, int>();
  7. for (var index = 0; index < nums.Length; index++)
  8. {
  9. var i = nums[index];
  10. if (dict.ContainsKey(i))
  11. {
  12. if (index - dict[i] <= k)
  13. return true;
  14. dict[i] = index;
  15. }
  16. else
  17. {
  18. dict.Add(i, index);
  19. }
  20. }
  21.  
  22. return false;
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement