Query on array from another array - iphone

I have the following 2 arrays. Array A with i.e. 10 id's, and the other array, Array B with 300 id's with all the corresponding data.
I want to retrieve all the data from B with id's which are stated in the array A.
I could just loop all entries in array B on every entry in array A, but this looks a little bit heavy for such simple task. What would be the best solutions in this case?
Thnx!

If you don't need an ordered array you could switch to an hash table (eg. NSSet) which has a much faster lookup time (strong bias towards O(1)). Otherwise you had to loop through the whole array and check all of its members.

Probably use a predicate (a, b are your arrays):
NSPredicate* filter = [NSPredicate predicateWithFormat: #"self IN %#", a];
NSArray* result = [b filteredArrayUsingPredicate: filter];
Although, it might only work if the objects in your arrays are simple things like strings.

When you are filling array B you can instead make it a dictionary. Assuming ids are unique you can then get value of the object for key in array A from array B. This will avoid looping and is sort of equivalent to hash table.
The returned value will then have the corresponding data for the particular unique id.

I don't see how you are going to do it otherwise if you are using arrays. The only way to access the right element is by looping through the array.

Related

How to Sort the Array in Min to Max in NetLogo

I want to sort the array in NetLogo. Like this {{array: 22 16 10 7}}
how can I achieve it?
Thanks
The array extension doesn't provide for sorting.
Consider using a list instead. Normally in NetLogo we use lists and agentsets for everything; using arrays at all is rare.
But, assuming you really need a sorted array, you could copy the array to a list, sort the list, then make a new array from the result. (Or store the sorted items back into the original array, one at a time.)
Did you try using sort-by? I guess that will do the magic.

Selectively remove and delete objects from a NSMutableArray in Swift

Basic question. What is the best way to selectively remove and delete items from a mutable Array in Swift?
There are options that do NOT seem to be suited for this like calling removeObject inside a
for in loop
enumeration block
and others that appear to work in general like
for loop using an index + calling removeObjectAtIndex, even inside the loop
for in loop for populating an arrayWithItemsToRemove and then use originalArray.removeObjectsInArray(arrayWithItemsToRemove)
using .filter to create a new array seems to be really nice, but I am not quite sure how I feel about replacing the whole original array
Is there a recommended, simple and secure way to remove items from an array? One of those I mentioned or something I am missing?
It would be nice to get different takes (with pros and cons) or preferences on this. I still struggle choosing the right one.
If you want to loop and remove elements from a NSMutableArray based on a condition, you can loop the array in reverse order (from last index to zero), and remove the objects satisfying the condition.
For example, if you have an array of integers and want to remove the numbers divisible by three, you can run the loop like this:
var array: NSMutableArray = [1, 2, 3, 4, 5, 6, 7];
for index in stride(from: array.count - 1, through: 0, by: -1) {
if array[index] as Int % 3 == 0 {
array.removeObjectAtIndex(index)
}
}
Looping in reverse order ensures that the index of the array elements still to check doesn't change. In forward mode instead, if you remove for instance the first element, then the element previously at index 1 will change to index 0, and you have to account for that in the code.
Usage of removeObject (which doesn't work with the above code) is not recommended in a loop for performance reasons, because its implementation loops through all elements of the array and uses isEqualTo to determine whether to remove the object or not. The complexity order raises from O(n) to O(n^2) - in a worst case scenario, where all elements of the array are removed, the array is traversed once in the main loop, and traversed again for each element of the array. So all solution based on enumeration blocks, for-in, etc., should be avoided, unless you have a good reason.
filter instead is a good alternative, and it's what I'd use because:
it's concise and clear: 1 line of code as opposed to 5 lines (including closing brackets) of the index based solution
its performances are comparable to the index based solution - it is a bit slower, but I think not that much
It might not be ideal in all cases though, because, as you said, it generates a new array rather than operating in place.
When working with NSMutableArray you shouldn't remove objects while you are looping along the mutable array itself (unless looping backwards, as pointed out by Antonio's answer).
A common solution is to make an immutable copy of the array, iterate on the copy, and remove objects selectively on the original mutable array by calling "removeObject" or by calling "removeObjectAtIndex", but you will have to calculate the index, since indexes in the original array and the copy will not match because of the removals (you will have to decrement the "removal index" each time an object is removed).
Another solution (better) is to loop the array once, create an NSIndexSet with the indexes of the objects to remove, and then call "removeObjectsAtIndexes:" on the mutable array.
See documentation on NSMutableArray's "removeObjectsAtIndexes:" in Swift.
Some of the options:
For loop over indexes and calling removeObjectAtIndex: 1) You will have to deal with the fact that when you remove, the index of the following object will become the current index, so you have to make sure to not increment the index in that case; you can avoid this by iterating backwards. 2) Each call to removeObjectAtIndex is O(n) (since it must shift all following elements forwards), so the algorithm is O(n^2).
For loop to build a set of elements to remove and then calling removeObjectsInArray: The first part is O(n). removeObjectsInArray uses a hash table to test elements for removal efficiently; hash table access is O(1) on average but O(n) worst-case, so the algorithm is O(n) on average, but O(n^2) worst-case.
Using filter to create a new array: This is O(n). It creates a new array.
For loop to build an index set of indexes of elements to remove (or with indexesOfObjectsPassingTest), then remove them using removeObjectsAtIndexes: I believe this is O(n). It does not create a new array.
Use filterUsingPredicate using a predicate based on a block of your test: I believe this is also O(n). It does not create a new array.

Searching for subrow in an array

I have a problem with swift. I want to search an array of strings consists of 3 different elements filling this array in certain manner. Next i want to search the array for a subset of 3 particular strings next to each other and return their indexes. Is that a special array method for this?
Copied from comment:
Assumed i have an Int array like [1,2,1,3,2]. I want to search it for a subarray [1,2,1] and return indexes of those elements. Should i use findwithPredicate method?
I would just iterate the Array checking for matches.
Check the items in the array in sequence comparing the current item with the first item of the subarray. If it does not match move on. If it does start comparing the rest of the subarray, if it fails go bad to scanning the array.

What's the easiest way to return the results of a query for a given key/value pair in mongo as an array of the values returned?

I have a field called id (not _id) in documents from two collections. I need to compare the contents of the first collection with the second. Basically, I need to know what documents with a given value 'id' exist in collection 'A', but not 'B'. What's the easiest way to build an array of id's from Collection A that I can use to do something like the following. :
db.B.find({id:{$nin: array_of_ids_from_coll_A}})
Please don't get hung up over why I'm using 'id' in this case, and not '_id'. Thanks.
Strictly speaking, this doesn't answer the question of 'how to build an array that...', but I'd iterate over collection A and, for each element, try to find a match in B. If none is found, add to a list.
This has a lot of roundtrips to the database, so it's not very fast, but it's very simple. Also, if A contains a lot of elements, the array of ids might be too large to throw all of them in the $nin, which otherwise would have to be solved by splitting up the array of ids. To make matters worse, $nin isn't efficient with indexes anyway.
I incorrectly assumed that the function 'distinct' returned a set of distinct documents based on a given 'field'. In fact, it returns an array of distinct values, provided a specific field. So, I was able to construct the array I was looking for with db.A.distinct('id'). Thanks to anyone who took the time to read this question, anyway.

How to exclusion join 2 NSArrays?

I've got 2 arrays of objects (as NSArray's) and I want to get an array of the objects in the first that aren't also in the 2nd.
See NSMutableArray's -removeObjectsInArray: method.
1) Inefficient solution. run over one array and call on each object containsObject if no do add the element otherwise take the next element
2) More efficient: sort both array and then step them side-by side. Keep a reference to the last added object and then compare the next components to this element. If none matches you can add a new "last" element.
3) Drop all the Elements in a HashTable then take the next elements of the next array and check whether you can find an "equal" object. At the end rebuild an Array from the HashTable
4) drop all the elements into an NSSet and after that rebuild an Array from this NSSet