[Solved] Avoid duplicates while adding in dictionary


Try this code to avoid duplication

I hope “id” value will be unique in your dictionary.

    var mydictionary = ["id": "1", "quantity": "","sellingPrice":""] as [String : Any]

    var arrayOfDictionary = [Dictionary<String, Any>]() //declare this globally
    let arrValue = arrayOfDictionary.filter{ (($0["id"]!) as! String).range(of: mydictionary["id"]! as! String, options: [.diacriticInsensitive, .caseInsensitive]) != nil }
    if arrValue.count == 0 {

        arrayOfDictionary.append(mydictionary)
    }

1

solved Avoid duplicates while adding in dictionary