2
let ItCompanies=["Facebook", "Google", "Microsoft", "Apple", "IBM", "Oracle" , "Amazon"]

let arr=[]
for(i=0;i<ItCompanies.length;i++){
    if(ItCompanies[i].includes("o")){
        arr.push(ItCompanies[i])
       
    }
    
}

for(i=0;i<arr.length;i++){
    let split=arr[i]
    for(i=0;i<split.length;i++){
        let split=arr[i].split("")
        if(split.indexOf("o")!= split.lastIndexOf("o")){
            console.log(arr[i])
        }
    }
    
}

Finding multiple duplicate letters in components within an array Can you share with me a shorter way of doing this?

1
  • couldn't this split.indexOf("o")!= split.lastIndexOf("o") go in the first loop (or something similar)? Commented Jan 10, 2023 at 18:11

1 Answer 1

1

I think you don't need to use arr on your code. Here is my solution:

let ItCompanies=["Facebook", "Google", "Microsoft", "Apple", "IBM", "Oracle" , "Amazon"]

for (let i = 0; i < ItCompanies.length; i++){
    if (ItCompanies[i].indexOf("o") != ItCompanies[i].lastIndexOf("o")){
        console.log(ItCompanies[i])
    }
}

Or you can use forEach method of javascript array like this:

let ItCompanies=["Facebook", "Google", "Microsoft", "Apple", "IBM", "Oracle" , "Amazon"]

ItCompanies.forEach((item) => {
    if (item.indexOf("o") != item.lastIndexOf("o")){
        console.log(item)
    }  
})
Sign up to request clarification or add additional context in comments.

1 Comment

If you are satisfied with my answer, can you give me a green check mark to my answer? ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.