Salah
Salah

Reputation: 953

How to remove duplicated objects from NSArray?

I have NSArray() which is include names but there's duplicated names how can i remove them ?

After parse query adding the objects to the NSArray and its duplicated

var names = NSArray()
let query = PFQuery(className: "test")
        query.whereKey("receivers", equalTo: PFUser.currentUser()!.username!)

        query.findObjectsInBackgroundWithBlock {
            (objects, error) -> Void in

            if error == nil {

                self.names = objects!
                let set = NSSet(array: self.names as [AnyObject])

                print(objects!.count)
                // count is 4
                // database looks like this (justin , kevin , kevin , joe)

Upvotes: 1

Views: 2904

Answers (3)

John Tracid
John Tracid

Reputation: 4046

If your names are strings you could create NSSet from array and it will have only different names.

let names = ["John", "Marry", "Bill", "John"]
println(names)

let set = NSSet(array: names)
println(set.allObjects)

prints:

"[John, Marry, Bill, John]"
"[Bill, John, Marry]"

Update #1
With new information in question (code fragment) it may look like this

var set = Set<String>()
for test in objects as [Test] {
    set.insert(test.sender)
}
self.names = Array(set)

Upvotes: 2

Avery Bentz
Avery Bentz

Reputation: 287

Here is a more complicated way to approach this that works. You could just run through a loop of the array and create a new one from the original. For example:

var check = 0

let originalArray:NSMutableArray = ["x", "y", "x", "z", "y", "z"]
let newArray: NSMutableArray = []
println(originalArray)

for var int = 0; int<originalArray.count; ++int{
    let itemToBeAdded: AnyObject = originalArray.objectAtIndex(int)
    for var int = 0; int<newArray.count; ++int{

        if (newArray == ""){
            break;

        }
        else if ((newArray.objectAtIndex(int) as! String) != itemToBeAdded as! String){

        }
        else if ((newArray.objectAtIndex(int) as! String) == itemToBeAdded as! String){
             check = 1
            break
        }


    }
    if (check == 0){
        newArray.addObject(itemToBeAdded)
    }
}

Basically I set a check var = 0. for every object in the originalArray, it loops through the newArray to see if it already exists and if it does the check var gets set to 1 and the object does not get added twice.

Upvotes: 0

Kelan
Kelan

Reputation: 2276

To expand on John's answer, an NSSet will, by definition, only contain a single copy of each object that hashes to be equal. So, a common pattern is to convert the array to a set and back.

This will work for any object type that has a reasonable implementation of -hash and -isEqual:. As John shows, String is one such object.

You could also do it with pure Swift:

let arrayWithDuplicates = [ "x", "y", "x", "x" ]
let arrayWithUniques = Array(Set(arrayWithDuplicates))  // => [ "y", "x" ]

But, it looks like you're already working with NSArray, so I think the John's example is more applicable.

Also, as my example shows, be aware that the order of the final array is not guaranteed to be in the same order as your original. If you want that, I think you can use NSOrderedSet instead of NSSet.

Upvotes: 1

Related Questions