Find Duplicate Elements In Array Using Swift
Solution 1:
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.
Solution 2:
To find duplicates, you could build cross reference by phone number, then filter that down to duplicates only. For example, consider:
let contacts = [
Contact(name: "Rob", phone: "555-1111"),
Contact(name: "Richard", phone: "555-2222"),
Contact(name: "Rachel", phone: "555-1111"),
Contact(name: "Loren", phone: "555-2222"),
Contact(name: "Mary", phone: "555-3333"),
Contact(name: "Susie", phone: "555-2222")
]
In Swift 4, you can build the cross reference dictionary with:
let crossReference = Dictionary(grouping: contacts, by: { $0.phone })
Or, in Swift 5.2 (thanks to SE-0249), you can do:
let crossReference = Dictionary(grouping: contacts, by: \.phone)
Or
let crossReference = contacts.reduce(into: [String: [Contact]]()) {
$0[$1.phone, default: []].append($1)
}
Then, to find the duplicates:
let duplicates = crossReference
.filter { $1.count > 1 } // filter down to only those with multiple contacts
.sorted { $0.1.count > $1.1.count } // if you want, sort in descending order by number of duplicates
Clearly use whatever model types make sense for you, but the above uses the following Contact
type:
struct Contact {
let name: String
let phone: String
}
There are many, many ways to implement this, so I wouldn't focus on the implementation details of the above, but rather focus on the concept: Build cross reference original array by some key (e.g. phone number) and then filter results down to just those keys with duplicate values.
It sounds like you want to flatten this structure that reflects the duplicates, into a single array of contacts (I'm not sure why you'd want to do that, as you lose the structure identifying which are duplicates of each other), but if you want to do that, you can flatMap
it:
let flattenedDuplicates = crossReference
.filter { $1.count > 1 } // filter down to only those with multiple contacts
.flatMap { $0.1 } // flatten it down to just array of contacts that are duplicates of something else
For Swift 2 or 3 renditions, see previous renditions of this answer.
Solution 3:
Swift 4+
2 line, fast solution:
var numbers = [1,2,3,4,5,6,6,6,7,8,8]
let dups = Dictionary(grouping: numbers, by: {$0}).filter { $1.count > 1 }.keys
//Results: [6, 8]
Solution 4:
Entirely derived from Rob's very neat answer. I've made it in to an Array
extension and given names to the intermediate steps for clarity:
extension Array where Element: Hashable {
func duplicates() -> Array {
let groups = Dictionary(grouping: self, by: {$0})
let duplicateGroups = groups.filter {$1.count > 1}
let duplicates = Array(duplicateGroups.keys)
return duplicates
}
}
[1, 2, 2, 3, 1].duplicates() -> [1, 2]
Solution 5:
You could implement it using "Merge sort", but you need to make one modification, during the merge step you should ignore the duplicates.
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.
var phoneNumbers = [123456, 234567, 345678, 123456, 456789, 135790, 456789, 142638]
func findDuplicates(sortedArray array: [Int]) -> [Int]
{
var duplicates: [Int] = []
var prevItem: Int = 0
var addedItem: Int = 0
for item in array
{
if(prevItem == item && addedItem != item)
{
duplicates.append(item)
addedItem = item
}
prevItem = item
}
return duplicates
}
func sortPhoneNumbers(phoneNumbers: [Int]) -> [Int]
{
return phoneNumbers.sorted({ return $0<$1 })
}
sortPhoneNumbers(phoneNumbers)
findDuplicates(sortPhoneNumbers(phoneNumbers))
In addition, you could implement the findDuplicates method in different ways:
Using Set (Swift 1.2+):
func findDuplicates(array: [Int]) -> [Int]
{
var duplicates = Set<Int>()
var prevItem = 0
for item in array
{
if(prevItem == item)
{
duplicates.insert(item)
}
prevItem = item
}
return Array(duplicates)
}
And so on.