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?
CodePudding user response:
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)
}
})