functionlucky(s){// split the string into an array so that it can be reducedletsarr=s.split('');// reduce the array into a character -> count mapletcounts=sarr.reduce((acc,c)=>{if(acc[c]){acc[c]++;}else{acc[c]=1;}returnacc;},{});letsmallestCount=Number.MAX_VALUE;// find the smallest frequency in the mapfor(letcincounts){if(Object.prototype.hasOwnProperty.call(counts,c)&&counts[c]<smallestCount){smallestCount=counts[c];}}letbiggerCount=0;// count how many characters exceed the smallest frequencyfor(letcincounts){if(Object.prototype.hasOwnProperty.call(counts,c)&&counts[c]>smallestCount){biggerCount+=counts[c]-smallestCount;}}// exceeding 0 times fullfills point 1leteasyLucky=biggerCount==0;// exceeding only once fullfills point 2lethardLucky=biggerCount==1;// if either is true you have a lucky stringreturneasyLucky||hardLucky;}
Object.prototype.hasOwnProperty.call(counts, c) is necessary in case the prototype for object has been messed with.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Object.prototype.hasOwnProperty.call(counts, c)
is necessary in case the prototype for object has been messed with.