We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Optimized using Hahsh map// TC: O(n) // SC: O(n)functionminimumDistances(a){lethash={};// number to its first index letminDist=Infinity;for(leti=0;i<a.length;i++){if(hash[a[i]]!==undefined){minDist=Math.min(minDist,Math.abs(i-hash[a[i]]));}else{hash[a[i]]=i;}}if(minDist===Infinity){return-1;}returnminDist;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Distances
You are viewing a single comment's thread. Return to all comments →