Untitled
unknown
plain_text
2 years ago
925 B
7
Indexable
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer,Integer> map=new HashMap<>();
for(int x:nums){
map.put(x,map.getOrDefault(x,0)+1);
}
System.out.println(map);
for(Integer i:map.keySet()){
if(map.get(i)>=2){
if(check(nums,i,k)==false){
return false;
}
}
}
return true;
}
public boolean check(int[] nums,int d,int k){
ArrayList<Integer> l=new ArrayList<>();
for(int i=0;i<nums.length;i++){
if(nums[i]==d){
l.add(i);
}
}
System.out.println(l);
for(int i=0;i<l.size()-1;i++){
if(Math.abs(l.get(i)-l.get(i+1))>k){
return false;
}
}
return true;
}
}Editor is loading...
Leave a Comment