Java O(n)t O(n)s


  • 0
    R
    public class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            Map<Integer,Integer> m=new HashMap<Integer,Integer>();
            for(int i=0;i<nums.length;i++){
                if(m.containsKey(nums[i])) if(Math.abs(i-m.get(nums[i]))<=k) return true;
                m.put(nums[i],i);
            }
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.