0

I'm trying to filter an array for duplicates:

for i in 0...arrCheck.count-1 {

            let searchNum = arrCheck[i]
            let filteredArray = arrCheck.indices.filter{arrCheck[$0] == searchNum}...

if I pass something like [2, 3, 3, 4, 5, 2] this works fine. But when I pass [2, 2] it blows up - filteredArray gets millions of items. Any idea where I am falling off the rails?

Thanks

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • See https://stackoverflow.com/a/40579948/3141234 – Alexander Oct 17 '17 at 17:27
  • 3
    Not related but please, this is not C, in Swift write `for i in 0.. – vadian Oct 17 '17 at 17:27
  • @vadian Actually, we write `for i in arrCheck.indices` :) – Alexander Oct 17 '17 at 17:27
  • @Alexander That's the next level ;-) – vadian Oct 17 '17 at 17:28
  • can you show the entire code? – TNguyen Oct 17 '17 at 17:35
  • Do you want to know what the duplicates are, or just remove them? If it's just removing them, why not cast the Array as a Set? – Adrian Oct 17 '17 at 17:59
  • Thanks for the two tips above. I'm not looking to remove the duplicates, just find them and which one has the lower index. So in the case I put in above, it would be 3/2 (three being the dup and 2 being it's second index occurrence, which is lower than 2/5 – PruitIgoe Oct 17 '17 at 18:02
  • 1
    "I'm not looking to remove the duplicates, just find them and which one has the lower index." Then that is what your question should state. You need to say clearly what you are trying to do, with sufficient specificity, and optimally with some sample input and desired output. Please edit your question to state _what the problem actually is_. – matt Oct 17 '17 at 18:06
  • Simple solution for removing duplicates: https://stackoverflow.com/a/35014912/1187415 – LinusGeffarth Oct 18 '17 at 12:17

0 Answers0