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

9
  • See stackoverflow.com/a/40579948/3141234 Commented Oct 17, 2017 at 17:27
  • 3
    Not related but please, this is not C, in Swift write for i in 0..<arrCheck.count Commented Oct 17, 2017 at 17:27
  • @vadian Actually, we write for i in arrCheck.indices :) Commented Oct 17, 2017 at 17:27
  • @Alexander That's the next level ;-) Commented Oct 17, 2017 at 17:28
  • 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. Commented Oct 17, 2017 at 18:06

0

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.