Remove duplicates from array of dictionaries, Swift 3 - ios

Problem
I have an array of dictionaries as follows:
var arrayOfDicts = [
["Id":"01", "Name":"Alice", "Age":"15"]
["Id":"02", "Name":"Bob", "Age":"53"]
["Id":"03", "Name":"Cathy", "Age":"12"]
["Id":"04", "Name":"Bob", "Age":"83"]
["Id":"05", "Name":"Denise", "Age":"88"]
["Id":"06", "Name":"Alice", "Age":"44"]
]
I need to remove all dictionaries where there is a duplicate name. For instance, I need an output of:
var arrayOfDicts = [
["Id":"01", "Name":"Alice", "Age":"15"]
["Id":"02", "Name":"Bob", "Age":"53"]
["Id":"03", "Name":"Cathy", "Age":"12"]
["Id":"05", "Name":"Denise", "Age":"88"]
]
Order does not need to be preserved.
Attempted Solution
for i in 0..<arrayOfDicts.count
{
let name1:String = arrayOfDicts[i]["Name"]
for j in 0..<arrayOfDicts.count
{
let name2:String = arrayOfDicts[j]["Name"]
if (i != j) && (name1 == name2)
{
arrayOfDicts.remove(j)
}
}
}
This crashes though, I believe since I am modifying the size of arrayOfDicts, so eventually it j is larger than the size of the array.
If someone could help me out, that would be much appreciated.

I definitely recommend having a new copy rather than modifying the initial array. I also create storage for names already used, so you should only need to loop once.
func noDuplicates(_ arrayOfDicts: [[String: String]]) -> [[String: String]] {
var noDuplicates = [[String: String]]()
var usedNames = [String]()
for dict in arrayOfDicts {
if let name = dict["name"], !usedNames.contains(name) {
noDuplicates.append(dict)
usedNames.append(name)
}
}
return noDuplicates
}

You can use a set to control which dictionaries to add to the resulting array. The approach it is very similar to the one used in these answer and this
let array: [[String : Any]] = [["Id":"01", "Name":"Alice", "Age":"15"],
["Id":"02", "Name":"Bob", "Age":"53"],
["Id":"03", "Name":"Cathy", "Age":"12"],
["Id":"04", "Name":"Bob", "Age":"83"],
["Id":"05", "Name":"Denise", "Age":"88"],
["Id":"06", "Name":"Alice", "Age":"44"]]
var set = Set<String>()
let arraySet: [[String: Any]] = array.compactMap {
guard let name = $0["Name"] as? String else { return nil }
return set.insert(name).inserted ? $0 : nil
}
arraySet // [["Name": "Alice", "Age": "15", "Id": "01"], ["Name": "Bob", "Age": "53", "Id": "02"], ["Name": "Cathy", "Age": "12", "Id": "03"], ["Name": "Denise", "Age": "88", "Id": "05"]]

Please check this answer:
var arrayOfDicts = [
["Id":"01", "Name":"Alice", "Age":"15"],
["Id":"02", "Name":"Bob", "Age":"53"],
["Id":"03", "Name":"Cathy", "Age":"12"],
["Id":"04", "Name":"Bob", "Age":"83"],
["Id":"05", "Name":"Denise", "Age":"88"],
["Id":"06", "Name":"Alice", "Age":"44"]
]
var answerArray = [[String:String]]()
for i in 0..<arrayOfDicts.count
{
let name1 = arrayOfDicts[i]["Name"]
if(i == 0){
answerArray.append(arrayOfDicts[i])
}else{
var doesExist = false
for j in 0..<answerArray.count
{
let name2:String = answerArray[j]["Name"]!
if name1 == name2 {
doesExist = true
}
}
if(!doesExist){
answerArray.append(arrayOfDicts[i])
}
}
}

Several good answers already, but it was a fun exercise, so here's my solution. I'm assuming you don't care which of the duplicate entries are kept (this will keep the last one of the dupes).
func noDuplicates(arrayOfDicts: [[String:String]]) -> [[String:String]]
{
var noDuplicates: [String:[String:String]] = [:]
for dict in arrayOfDicts
{
if let name = dict["name"]
{
noDuplicates[name] = dict
}
}
// Returns just the values of the dictionary
return Array(noDuplicates.values.map{ $0 })
}

let uniqueArray = Array(Set(yourArrayWithDuplicates))
That should do the trick.
If you want to use just the name for uniqueness then create these as structs.
You shouldn't be doing anything with dictionaries. Much easier to work with data that makes sense.

Try this:
var uniqueNames = [String: [String:String] ]()
for air in arrayOfDicts {
if (uniqueNames[arr["Name"]!] == nil) {
uniqueNames[arr["Name"]!] = arr
}
}
result = Array(uniqueNames.values)

If you don't mind using an additional list:
var uniqueArray = [[String: String]]()
for item in arrayOfDicts {
let exists = uniqueArray.contains{ element in
return element["Name"]! == item["Name"]!
}
if !exists {
uniqueArray.append(item)
}
}

Related

Create Dictionary from Array in Swift 5

I need to create a dictionary from array with custom type for first index of the array.
Sample array : ["ABC","ZYZ","123"]
Required result : [{"name" : "ABC", "type:"A"},{"name" : "ZYZ", "type:"B"},{"name" : "123", "type:"B"}]
Note type A for first index.
My code
for url in urlArray {
urlDict["name"] = url
}
You can do a map, and then individually change the type of the first dictionary:
var dicts = urlArray.map { ["name": $0, "type": "B"] }
dicts[0]["type"] = "A"
Seeing how all your dictionary keys are all the same, and that you are sending this to a server, a Codable struct might be a better choice.
struct NameThisProperly : Codable {
var name: String
var type: String
}
var result = urlArray.map { NameThisProperly(name: $0, type: "B") }
result[0].type = "A"
do {
let data = try JSONDecoder().encode(result)
// you can now send this data to server
} catch let error {
...
}
I suppose you can use a high order function such as map or reduce
Here is an example using reduce
var array = ["ABC","ZYZ","123"]
var result = array.reduce([[String: String]](), { (previous, current) -> [[String: String]] in
let type = previous.count == 0 ? "A" : "B"
let dictForCurrent = [
"name": current,
"type": type
]
return previous + [dictForCurrent]
})
print(result)
The result:
[["type": "A", "name": "ABC"], ["type": "B", "name": "ZYZ"], ["name":
"123", "type": "B"]]
Use reduce to convert array to dictionary:
let resultDict: [String: String]
= array.reduce(into: [:]) { dict, url in
dict["name"] = url
}
The result will look like:
[
"name": URL1,
"name": URL2
]
Use map(_:) to convert each element of the array to dictionary like so,
let arr = ["ABC","ZYZ","123"]
let result = arr.map { (element) -> [String:String] in
var dict = [String:String]()
dict["name"] = element
if let char = element.first {
dict["type"] = String(char)
}
return dict
}
print(result)
since you are concern about the index, my approach will be using enumerated() which gives out the index
let array = ["ABC","ZYZ","123"]
var results: [[String: String]] = []
for (i, content) in array.enumerated() {
let type: String = i == 0 ? "A" : "B"
results.append(["name": content, "type": type])
}
print(result)
// [["type": "A", "name": "ABC"], ["name": "ZYZ", "type": "B"], ["type": "B", "name": "123"]]

How to append JSON values into array using Swift 4?

I am trying to get JSON values and appending into array. Here, below code add_user_product have a chance to come null. If it is null need to append null into array and if not null need to store ID also.
I am trying to get output like - [10,null,12,13,null,……]
// add_user_products & If add_user_product == null need to store null otherwise add_user_product ["id"]
if let add_user_product = fields[“add_user_product"] as? [String : Any] {
let add_id = add_user_product["id"] as! Int
self.addlistIDData.append(add_id)
}
else {
//print("Failure")
}
below my sample response
{
"students":[
{
"grade":0,
"add_user_product":
{
"id":10
}
},
{
"grade":1,
"add_user_product":null
},
{
"grade":2,
"add_user_product":
{
"id":11
}
}
]
}
Expected output: [10,null,11,......] //This array I am going to use Tableview cell
I suggest use nil instead of null string.
Declare your addlistIDData type as [Int?] where Int is an Optional.
Consider below example I have created for you:
var addlistIDData: [Int?] = [10, nil, 12, 13, nil] //Created array just for example
//created dict for testing purpose
let fields: [String : Any]? = ["add_user_product": ["id": nil]]
if let field = fields {
if let add_user_product = field["add_user_product"] as? [String:Any] {
let add_id = add_user_product["id"] as? Int
//now append your object here weather it's a nil or it has any value
addlistIDData.append(add_id)
}
}
else {
//print("Failure")
}
print(addlistIDData)
And output will be:
[Optional(10), nil, Optional(12), Optional(13), nil, nil]
PS: You need to cast an object with if let or with guard let whenever you are accessing objects from this addlistIDData array.
null will not be identifiable, the only way to store it in your array would be to store it as String, But for that also you'll have to store othere elements as String.
But i would suggest instead of adding null just add 0 as:
var arr = [Int]()
if let add_user_product = fields["add_user_product"] as? [String: Any] {
if let productId = add_user_product["id"] as? Int {
arr.append(productId)
} else {
arr.append(0)
}
} else {
//
}
You can do like this:
var resultArray = [Int?]()
if let add_user_product = fields["add_user_product"] as? [String: Any] {
if let add_id = add_user_product["id"] as? Int {
resultArray.append(add_id)
} else {
resultArray.append(nil)
}
} else {
//print("Failure")
}
Hope this Helps.
You can use compactMap:
let arrayDict = [ ["id" : 3], ["id" : nil], ["id" : 5] ]
let result = arrayDict.compactMap { $0["id"] }
print(result)
Output:
[Optional(3), nil, Optional(5)]

Remove duplicate values from a dictionary in Swift 3

Hi I have a dictionary and I just wanna remove duplicate values (with their keys) like so :
var myDict : [Int:String] = [1:"test1", 2:"test2", 3:"test1", 4:"test4"]
Desired output :
[1: "test1", 2: "test2", 4: "test4"]
It looks to me like all the other answers will have O(n^2) performance.
Here is a solution that should operate in O(n) time:
var sourceDict = [1:"test1", 2:"test2", 3:"test1", 4:"test4"]
var uniqueValues = Set<String>()
var resultDict = [Int:String](minimumCapacity: sourceDict.count)
//The reserveCapacity() function doesn't exist for Dictionaries, as pointed
//out by Hamish in the comments. See the initializer with minimumCapacity,
//above. That's the way you have to set up a dictionary with an initial capacity.
//resultDict.reserveCapacity(sourceDict.count)
for (key, value) in sourceDict {
if !uniqueValues.contains(value) {
uniqueValues.insert(value)
resultDict[key] = value
}
}
For small dictionaries the difference is insignificant, but if you have a dictionary with hundreds (or thousands) of key/value pairs then the performance of an n^2 algorithm starts to get really bad.
var myDict: [Int:String] = [1:"test1", 2:"test2", 3:"test1", 4:"test4"]
var result: [Int:String] = [:]
for (key, value) in myDict {
if !result.values.contains(value) {
result[key] = value
}
}
print(result)
This is another way of doing the same
var myDict : [Int:String] = [1:"test1", 2:"test1", 3:"test1", 4:"test4", 5:"test4"]
var newDict:[Int: String] = [:]
for (key, value) in myDict {
print(key, value)
let keys = myDict.filter {
return $0.1.contains(value)
}.map {
return $0.0
}
if keys.first == key {
newDict[key] = value
}
}
print(newDict)
You can use this code
let newDict = myDict.keys.sorted().reduce([Int:String]()) { (res, key) -> [Int:String] in
guard let value = myDict[key], !res.values.contains(value) else { return res }
var res = res
res[key] = value
return res
}
Please keep in mind that dictionary are not sorted so the output could be something like this
[2: "test2", 4: "test4", 1: "test1"]
Please refer to the answer provided by #Duncan for a faster solution.

Sorting of an array alphabetically in swift

I am new to swift.I am trying one sample app in which I need to implement the sorting of an array in alphabetical order.I getting the json data and I am adding the titles in the array.Now i would like to sort that alphabetically.Here is my code .....
func updateSearchResults(data: NSData?)
{
do
{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let blogs: NSArray = json["results"] as? [AnyObject] {
print(blogs)
for blog in blogs {
if let name = blog["original_title"] as? String {
names.addObject(name)
}
}
print(names)
**let sortedArray = sorted(names, {
(str1: String, str2: String) -> Bool in
return str1.toInt() < str2.toInt()** // Here I am getting the Error Message
})
}
}
catch {
print("error serializing JSON: \(error)")
}
}
The error message I am getting is "Cannot invoke 'sorted' with an argument list of type '(NSMutableArray, (String, String) -> Bool)'"
I tried a lot to achieve this but I didn't find the solution.
Can anyone help me to resolve this issue.
Thanks In Advance.
First convert NSMutableArray to the Array by using below line of code.
let swiftArray = mutableArray as AnyObject as! [String]
Use below line of code to sort the Array.
var sortedArray = names.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
Check below link for sort Closures.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
Update for Swift 3.0
var sortedArray = swiftArray.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }
Use this simple code of line to sort ur array
let sortedNames = names.sort { $0.name < $1.name }
For Swift 4 you can use only this
let sortedNames = names.sorted(by: <)
Swift4
var names = [ "Alpha", "alpha", "bravo", "beta"]
var sortedNames = names.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }
print(sortedNames) //Logs ["Alpha", "alpha","beta", "bravo"]
Swift 4(working code)
JSON response -> Stored in aryNameList
"DATA": [
{
email = "iosworker#gmail.com";
firstname = Harvey
},
{
email = "poonam#openxcell.com";
firstname = poonam
},
{
email = "t#t.com";
firstname = rahul
},
{
email = "android.testapps#gmail.com";
firstname = Chulbulx
},
{
email = "t#t2.com";
firstname = rahul
},
{
email = "jaystevens32#gmail.com";
firstname = Jay
},
{
email = "royronald47#gmail.com";
firstname = Roy
},
{
email = "regmanjones#hotmail.com";
firstname = Regan
},
{
email = "jd#gmail.com";
firstname = Jaydip
}
]
Code
self.aryNameList = self.aryNameList.sorted(by: { (Obj1, Obj2) -> Bool in
let Obj1_Name = Obj1.firstname ?? ""
let Obj2_Name = Obj2.firstname ?? ""
return (Obj1_Name.localizedCaseInsensitiveCompare(Obj2_Name) == .orderedAscending)
})
working every case (for ex: lowerCase, upperCase..)
For an array of objects:
items = items.sorted(by: { (item1, item2) -> Bool in
return item1.product.name.compare(item2.product.name) == ComparisonResult.orderedAscending
})
Try this one
var names = [ "Alpha", "alpha", "bravo"]
var sortedNames = names.sort { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
print(sortedNames) //Logs ["Alpha", "alpha", "bravo"]
Swift 3 solution:
let yourStringArray = [ "beTA", "ALPha", "Beta", "Alpha"]
var sortedArray = yourStringArray.sorted()
// Result will be ["ALPha", "Alpha", "Beta", "beTA"]
Creds to jjatie

Swift 2 - Separating an array into a dictionary with keys from A to Z

I have an array, for instance ["Apple", "Banana", "Blueberry", "Eggplant"] and I would like to convert it to a dictionary like follows:
[
"A" : ["Apple"],
"B" : ["Banana", "Blueberry"],
"C" : [],
"D" : [],
"E" : ["Eggplant"]
]
I am using Swift 2 on Xcode 7 beta 4. Thanks!
Using only Swift 2 objects and methods, and with a key for each letter in the alphabet:
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.map({ String($0) })
let words = ["Apple", "Banana", "Blueberry", "Eggplant"]
var result = [String:[String]]()
for letter in alphabet {
result[letter] = []
let matches = words.filter({ $0.hasPrefix(letter) })
if !matches.isEmpty {
for word in matches {
result[letter]?.append(word)
}
}
}
print(result)
I composed this in Xcode playground:
import Foundation
var myArray = ["Apple", "Banana", "Blueberry", "Eggplant"]
var myDictionary : NSMutableDictionary = NSMutableDictionary()
for eachString in myArray as [NSString] {
let firstCharacter = eachString.substringToIndex(1)
var arrayForCharacter = myDictionary.objectForKey(firstCharacter) as? NSMutableArray
if arrayForCharacter == nil
{
arrayForCharacter = NSMutableArray()
myDictionary.setObject(arrayForCharacter!, forKey: firstCharacter)
}
arrayForCharacter!.addObject(eachString)
}
for eachCharacter in myDictionary.allKeys
{
var arrayForCharacter = myDictionary.objectForKey(eachCharacter) as! NSArray
print("for character \(eachCharacter) the array is \(arrayForCharacter)")
}
I found this question helped me better understand some concepts which I had been thinking about. Here is an alternative take based on the accepted correct answer which is slightly more concise and where the alphabet is generated programmatically. This is Swift 2 in Xcode 7.
let words = ["Apple", "Banana", "Blueberry", "Eggplant"]
let alphabet = (0..<26).map {n in String(UnicodeScalar("A".unicodeScalars["A".unicodeScalars.startIndex].value + n))}
var results = [String:[String]]()
for letter in alphabet {
results[letter] = words.filter({$0.hasPrefix(letter)})
}
print(results)
I believe but am not certain that the let alphabet line could be made more concise.
Here's my solution. Works in pure Swift 2 and in O(n) time where n is the length of the list of words (and assuming a dictionary is implemented as a hash table).
var dictionary: [String : [String]] = [ "A" : [], "B" : [], "C" : [], "D" : [],
"E" : [], "F" : [] /* etc */ ]
let words = ["Apple", "Banana", "Blueberry", "Eggplant"]
for word in words
{
let firstLetter = String(word[word.startIndex]).uppercaseString
if let list = dictionary[firstLetter]
{
dictionary[firstLetter] = list + [word]
}
else
{
print("I'm sorry I can't do that Dave, with \(word)")
}
}
print("\(dictionary)")
I have just made such useful Array Extension that enables to map Array of Objects to Dictionary of Character Indexed Objects based on selected property of object.
extension Array {
func toIndexedDictionary(by selector: (Element) -> String) -> [Character : [Element]] {
var dictionary: [Character : [Element]] = [:]
for element in self {
let selector = selector(element)
guard let firstCharacter = selector.firstCharacter else { continue }
if let list = dictionary[firstCharacter] {
dictionary[firstCharacter] = list + [element]
} else {
// create list for new character
dictionary[firstCharacter] = [element]
}
}
return dictionary
}
}
extension String {
var firstCharacter : Character? {
if count > 0 {
return self[startIndex]
}
return nil
}
}

Resources