Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Duplicate Elements In Array Using Swift

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.

like image 585
C0mrade Avatar asked Apr 19 '15 08:04

C0mrade


People also ask

How do I find duplicates in Swift?

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."

How do I filter an array in Swift?

To filter an array in Swift: Call the Array. filter() method on an array. Pass a filtering function as an argument to the method.


1 Answers

Feeling ~clever~. Given an array of Ints

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.

like image 142
Patrick Perini Avatar answered Sep 19 '22 23:09

Patrick Perini