Home How to check there's duplicate value in array or not?
Reply: 5

How to check there's duplicate value in array or not?

Pankil
1#
Pankil Published in 2018-01-13 08:25:52Z
var arrEmails = NSMutableArray()
arrEmails = ["a.a@gmail.com", "a.a1@gmail.com", "a.a@gmail.com", "b.b@gmail.com", "c.c@gmail.com", "a.a1@gmail.com"]

above is my Array and i want to check is there any duplicate value or not. and for that i write below code.

let set = NSCountedSet.init(array: arrEmails as! [Any])
        var duplicates: Int = 0

        for var object in set {
            if set.count(for: object) > 1 {
                duplicates = duplicates+1
            }
        }

but in above code, if array contains values like below than it returns me duplicates value 1.

arrEmails = ["a.a@gmail.com", "", "", "b.b@gmail.com", "", ""]

I don't want to remove duplicate value, i just want to check number of duplicate value in array. How can check? please help me out.

Madhur
2#
Madhur Reply to 2018-01-13 08:30:42Z

your code is fine ! and you are getting the correct and expected result!

As in your code, the value which is duplicate is " ".

For your more understanding, you can change the array of values and can try again.

dasblinkenlight
3#
dasblinkenlight Reply to 2018-01-13 08:37:23Z

Your code counts the number of distinct duplicates. In your case all four duplicates are identical to each other - i.e. empty strings "".

If you want to count the total number of objects that have duplicates, add up the values returned from set.count(for: object):

let arrEmails = ["a.a@gmail.com", "", "", "b.b@gmail.com", "", ""]
let set = NSCountedSet.init(array: arrEmails as! [Any])
let totalDups = set.map { set.count(for: $0) }.filter {$0 > 1}.reduce(0, +)

The above code produces totalDups of 4.

Your code for counting unique duplicates could be reduced to a single line, as follows:

let uniqueDups = set.map { set.count(for: $0) }.filter {$0 > 1}.count
Luca Angeletti
4#
Luca Angeletti Reply to 2018-01-13 13:12:44Z

First of all stop using NSMutableArray and use Swift array.

So you have this array

let emails = ["a.a@gmail.com", "a.a1@gmail.com", "a.a@gmail.com", "b.b@gmail.com", "c.c@gmail.com", "a.a1@gmail.com"]

Now you can check whether there are duplicates simply writing

let hasDuplicates = emails.count != Set(emails).count

Note, this does work only if the generic type of the array is Hashable.

Vikky
5#
Vikky Reply to 2018-01-13 14:02:44Z

SWIFT 4

There are surely better ways to do this but i have one which i used to solve this type of problem

This is tested you can paste this code in playground for instant results

    // here we can store dulicate elements in array
    var storeDuplicateValue =  [String:Int]()
    let arrEmails = ["a.a@gmail.com", "", "", "b.b@gmail.com", "", "","b.b@gmail.com"]
        var count = 0
       // here we loop through all elements
        for email in arrEmails {
          if(email != ""){
        count = 0
         for newEmail in arrEmails {
       if(email == newEmail){
          count += 1

        }
    }
    // if count is more than 1 we have duplicate elements
    if(count > 1){
        // so we store duplicate elements in dictionary and its count to know how many times it has been repeated
        storeDuplicateValue.updateValue(count, forKey: email)
    }
}
}

print(storeDuplicateValue)
// here we get count of keys in dictionary to know number of duplicate strings
print(storeDuplicateValue.keys.count)
dr_barto
6#
dr_barto Reply to 2018-01-14 09:51:33Z

As stated by most other answers, your code is fine. If the only problem is that empty strings should be ignored, you can filter them out from you array before counting duplicates:

let emails = ["a.a@gmail.com", "", "", "b.b@gmail.com", "", ""]
let emailsWithoutEmptyStrings = emails.filter { $0 != "" }
let set = NSCountedSet(array: emailsWithoutEmptyStrings)
var duplicates = 0

for object in set {
  if set.count(for: object) > 1 {
    duplicates += 1
  }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.632579 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO