I get the right output in the console but I believe the performance is a bit too slow to pass. What can I do to optimize my solution?

I also was confused about why there would be duplicates in my amicNums. I saw this when I played with the solution in the console and realized this was what was giving me the wrong output originally so I added `amicNums.indexOf(i) === -1`

in the if-statement to fix this…however, I’m still not sure why there would be any duplicates?

**Your code so far**

```
function sumAmicableNum(n) {
function d(n){
let output = []
for (let i = 1; i < n; i++){
if (n%i===0){
output.push(i)
}
}
if (output.length === 0){
return 0
}else {
return output.reduce((a,b)=> a+=b)
}
}
let amicNums = []
for (let i = 1; i < n; i++){
let r1 = d(i)
let r2 = d(r1)
if (i === r2 && i !== r1 && amicNums.indexOf(i) === -1){
amicNums.push(i, r1)
}
}
return amicNums.reduce((a,b)=> a+=b)
}
sumAmicableNum(10000);
```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36`

.

**Link to the challenge:**

https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-21-amicable-numbers/