For example: “Today, is the greatest day ever!” should return greatest because it has 2 e’s (and 2 t’s) and it comes before ever which also has 2 e’s. If there are no words with repeating letters return -1. Words will be separated by spaces.
Input:”Hello Apple Pie”
Output should be:”Hello”
Do not understand what is wrong with my code, also if you think there is a simpler and shorter way to solve I would love to hear it. Thanks in advance for the help.
function LetterCountI(str) {
let unique=[... new Set(str)]
if(unique==str){
return -1}
let arr= str.split(" ")
let array=arr.map(item=>{
let temparr=item.split("")
return temparr.reduce((acc,curr)=>{
acc[curr]=acc[curr]? acc[curr]+1:1
if(acc[curr]>acc.max){
acc.max=acc[curr]}
return acc},{max:1, word:item})})
let amount=1
let largest=""
for(let item of arr){
if( item.max>amount){
amount=item.max
largest=item.word
}
}
return largest
}
