How to find Duplicate Elements in Array? I have array of phone numbers so in the phone numbers i should start searching from the right side to the left side and find similar 6 integers. then i should print them out.
The easiest way to find duplicate elements is if the phone number is just a 6-digit number and has type Int, you could sort the array of phone numbers and than filter that to find duplicates. And so on. @Mazyod - from the answer: "you could SORT the array of phone numbers and than FILTER that to find duplicates."
To filter an array in Swift: Call the Array. filter() method on an array. Pass a filtering function as an argument to the method.
Feeling ~clever~. Given an array of Int
s
let x = [1, 1, 2, 3, 4, 5, 5] let duplicates = Array(Set(x.filter({ (i: Int) in x.filter({ $0 == i }).count > 1}))) // [1, 5]
Please note, this is horrendously inefficient for everyone involved, including the compiler, and you.
I'm just showing off.
Edit: lol someone downvoted this, which leads me to reiterate, just in case: please DO NOT USE THIS in production or anywhere else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With