Compare 2 arrays of strings - comparison

I got 2 arrays of strings:
NSArray * current = #[#"1", #"6", #"53"];
NSArray * new = #[#"1", #"626", #"53", #"13"];
I want to get number 6 in array, and numbers 626 and 13 in second array
(I want data that is in first array but there is not in second, and conversely)
NSMutableSet * newSet = [NSMutableSet setWithArray:new];
[newSet minusSet:[NSSet setWithArray:current ]];
NSArray * result1 = [NSArray arrayWithSet:newSet];
NSArray * result2 = ?
I not get it, I know that is very simple question, but I have no ideas

Your code contains a lot of syntax errors.
Please post actual code, I understand the issue you are talking about, that's why I'm posting this answer. Please post questions with valid code, else you won't get proper answer (Only get downvotes)
Use the following code:
NSArray *current = #[#"1", #"6", #"53"];
NSArray *newArr = #[#"1", #"626", #"53", #"13"];
NSMutableSet *newSet = [NSMutableSet setWithArray:newArr];
[newSet minusSet:[NSSet setWithArray:current]];
NSArray * result1 = [NSArray arrayWithObjects:[newSet allObjects],nil];
// result 1 will have 626 and 13
newSet = [NSMutableSet setWithArray:current];
[newSet minusSet:[NSSet setWithArray:newArr]];
NSArray * result2 = [NSArray arrayWithObjects:[newSet allObjects],nil];
// result 2 will have 6

// Create arrays of the IDs only
NSArray *notiCloudIDs = [notiCloud valueForKey:#"id"];
NSArray *notiLocIDs = [notiLoc valueForKey:#"id"];
// Turn the arrays into sets and intersect the two sets
NSMutableSet *notiCloudIDsSet = [NSMutableSet setWithArray:notiCloudIDs];
NSMutableSet *notiLocIDsSet = [NSMutableSet setWithArray:notiLocIDs];
[notiCloudIDsSet intersectSet:notiLocIDsSet];
// The IDs that are now in notiCloudIDsSet have been present in both arrays
NSLog(#"Duplicate IDs: %#", notiCloudIDsSet);
This will give you the common elements in the two. You can then delete the common elements from array1.

Related

Sorting an NSArray keeping original indexes rather than values

I have an array of values, and I'd like to sort it from highest to lowest values. Here's my array:
#[#0.985517248005697843460382, #0.000103821940243745174581, #0.002930049254083499140483, #0.006089428685598983863325, #0.000169959081717878927225 #0.038708805305937427077012, #0.005785644142951103588435, #0.003949420720490224266663 #0.003306789895982742942537, #0.005520713777168946394258];
What's the most efficient way to create a new array, but instead of containing the sorted values, it would contain the indexes from the original array?
Currently this is what I'm doing:
NSArray *array = #[#3, #5, #7, #2, #4, #4.1, #1, #10];
NSArray *sortedArray = [array sortedArrayUsingSelector: #selector(compare:)];
NSMutableArray *arraySortedByOriginalIndexes = [[NSMutableArray alloc] init];
for (int i = 0; i < sortedArray.count; i++) {
NSUInteger index = [array indexOfObject:sortedArray[i]];
[arraySortedByOriginalIndexes addObject:#(index)];
}
Here is the simple solution if no numbers in array are equal.
NSArray *sortedArray = [array sortedArrayUsingSelector: #selector(compare:)];
NSMutableArray *mutableIndexes = [NSMutableArray array];
for (NSNumber *number in sortedArray) {
[mutableIndexes addObject:#([array indexOfObject:number])];
}
NSArray *indexes = [mutableIndexes copy];
Again, it only works when no numbers are equal.

Create array by comparing JSON data to an array

I'm getting access to 'hotel' from json file:
NSArray *hotel = [jsonObject valueForKeyPath:#"location.hotel"];
With print out like (UPDATED):
<__NSCFArray 0x7b254d70>(
{
test = A;
hotel = (
{
luxuryHotels = hotel1;
},
{
luxuryHotels = hotel2;
},
I want to check if the array 'hotel' contains objects from another array 'myHotels' and if it does, create an array with only those objects.
NSArray *myHotels = [NSArray arrayWithArray:hotel.luxuryHotels];
NSMutableArray *hotel2 = [[NSMutableArray alloc] init];
for (NSArray *object in hotel) {
NSMutableSet *set1 = [NSMutableSet setWithArray: object];
NSSet *set2 = [NSSet setWithArray: myHotels];
[set1 intersectSet: set2];
NSArray *resultArray = [set1 allObjects];
NSLog(#"resultArray is %#",resultArray);
[hotel2 addObject:resultArray];
}
The print for 'hotel2' is something like: (
),( hyatt
),(
)
Is there a way to get a string of only hyatt? Or a way to combine the dictionaries of the array so it show as on list if there are more than one hotel listed?
It's still not 100% clear what the data structure looks like but you could try something like this
Collapse your initial data structure (array of arrays) into just an array, then convert this into a mutable set
NSArray *flattenedHotels = [jsonObject valueForKeyPath:#"#distinctUnionOfArrays.location.hotel"];
NSMutableSet *hotels = [NSMutableSet setWithArray:flattenedHotels];
Next you just perform an intersection with myHotels
[hotels intersectSet:[NSSet setWithArray:myHotels]];
NSArray *result = hotels.allObjects;

get distinct value from NSmutablearray [duplicate]

This question already has answers here:
The best way to remove duplicate values from NSMutableArray in Objective-C?
(14 answers)
Closed 9 years ago.
i have a nsmutablearray as is shown below. I would like to retrieve list value without redundancy. in this example i should retrive 10 20 30
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:#"10",#"20",#"30",#"30",#"30",#"30",#"20", nil];
Transform the array into a set, and then back into an array.
NSSet *set = [NSSet setWithArray:array];
NSArray *a = [set allObjects];
You can also have this new array sorted:
NSArray *a2 = [a sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
Since iOS 5 you can use -[NSOrderedSet orderedSetWithArray:] which preserves the order:
NSArray *a2 = [[NSOrderedSet orderedSetWithArray:array] array];
NSArray *withRedunecy = [[NSSet setWithArray: array] allObjects];
This will be the one way like you can create new NSArray which has no duplicate objects or you need to create a logic for getting unique objects like insert one by one with checking is it already present or not.
Try to use this on
NSArray *array = #[#"10",#"20",#"30",#"30",#"30",#"30",#"20"];
NSArray *newArray = [[NSSet setWithArray:array] allObjects];
NSLog(#"%#", newArray);
Output :
(
30,
20,
10
)
Only this line of code will work fine .
NSSet *mySet = [NSSet setWithArray:array];
now mySet will have unique elements.so create array with this set
NSArray *myarray = [mySet allObjects];

Complement of two arrays in Objective-C

Very simple problem, I am on the right track, I have to use predicates but couldn't find the solution.
I have two arrays:
A = [1, 2, 3, 4]
B = [1, 2]
I want to have an aray C = A \ B that is
C = [3, 4]
Use the right tool for the task. What you're describing here is the set operation "difference".
NSMutableSet *first = [NSMutableSet setWithObjects:#1, #2, #3, #4, nil];
NSSet *second = [NSSet setWithObjects:#1, #2, nil];
[first minusSet:second];
If you really really really need to work with arrays, you can convert between arrays and sets too:
NSArray *array = [someSet allObjects];
or
NSSet *set = [NSSet setWithArray:someArray];
You can achieve it with removeObjectsFromArray:
NSArray * arrayA = #[ #1, #2, #3, #4 ];
NSArray * arrayB = #[ #1, #2 ];
NSMutableArray * arrayC = [arrayA mutableCopy];
[arrayC removeObjectsInArray:arrayB];
EDIT
As suggested in the comments and also proposed by H2CO3, if you don't have a valid reason to use NSArray, the most convenient solution is to change data structure and switch to NSSet, which has the support for set difference, using the method -minusSet:.
EDIT 2
As Sven noted in the comments, your objects must respond properly to hash and isEqual: in order to use removeObjectsInArray:. In the above example everything will work just fined, since NSNumber has a default implementation for those methods, but in case you're using a custom class you need to explicitly override them.
Here's another approach:
NSArray * arrayA = #[ #1, #2, #3, #4 ];
NSArray * arrayB = #[ #1, #2 ];
NSIndexSet *indexSet = [arrayA indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
if([arrayB containsObject:obj]){
return NO;
}
else{
return YES;
}
}];
NSArray *arrayC = [arrayA objectsAtIndexes:indexSet];

How to create NSDictionary inside NSArray and How to access them in ios

I am new to iOS and want to create an NSArray like this which contains an NSDictionary.
[
{
Image: 1, 2,3
Title: 1,2,3
Subtitle:1,2,3
}
]
I have tried this.
NSArray *obj-image=#[#"Test.png",#"Test.png",#"Test.png"];
NSArray *obj-title=#[#"Test",#"Test",#"Test"];
NSArray *obj-subtitle=#[#"Test",#"Test",#"Test"];
NSDictionary * obj_dictionary ={ image : obj_image, title:obj_title, subtitle:obj_subtitle}
NSArray * obj_array= [obj_dictionarry];
But not working and how to access them.
First of all, you initialization of Arrays and Dictionaries is wrong. You cannot use "-" in the names, period.
Second, you need to allocate and then initialize the objects. This is how you do that with arrays:
NSArray *images = [NSArray arrayWithObjects: #"TestImage",#"TestImage",#"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: #"TestTitle",#"TestTitle",#"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: #"TestSubTitle",#"TestSubTitle",#"TestSubTitle",nil];
Then you need Mutable dictionary and mutable arrays to work with the data (mutable means you can change the values inside, add or remove objects etc.)
This is the most basic example of what you are trying to achieve:
NSArray *images = [NSArray arrayWithObjects: #"TestImage",#"TestImage",#"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: #"TestTitle",#"TestTitle",#"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: #"TestSubTitle",#"TestSubTitle",#"TestSubTitle",nil];
NSMutableArray *objectsMutable = [[NSMutableArray alloc] init];
for (NSString *string in images) {
NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc] init];
[dictMutable setObject:string forKey:#"image"];
//determining the index of the image
NSInteger stringIndex = [images indexOfObject:string];
[dictMutable setObject:[titles objectAtIndex:stringIndex] forKey:#"title"];
[dictMutable setObject:[subtitles objectAtIndex:stringIndex] forKey:#"subtitle"];
NSDictionary *dict = [[NSDictionary alloc] init];
dict = dictMutable;
[objectsMutable addObject:dict];
}
NSArray *objects = objectsMutable;
NSLog(#"%#", objects);
Hope this helps.
As you can see, I'm going through the images array, capturing the index of each one, an then just apply values of other arrays from the same index into a mutable dictionary.
All I do after that is just make a regular dictionary and array to put the data inside. This is ho the Log will look:
(
{
image = TestImage;
subtitle = TestSubTitle;
title = TestTitle;
},
{
image = TestImage;
subtitle = TestSubTitle;
title = TestTitle;
},
{
image = TestImage;
subtitle = TestSubTitle;
title = TestTitle;
}
)
You have an array with three objects inside, each with their own image, title and subtitle.
Here is code :
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *mdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"object1",#"key1",#"object2",#"key2",nil];
[array addObject:mDict];
so now if u need to access dictionary from array then :
NSMutableDictionary *mDict1 = [array objectatindex:0];
NSLog(#"%#",[mDict1 valueForkey:#"key1"];
--> print object 1.
This is the way to store array of dictionaries:
NSDictionary *dic=#{#"kishore":#"hai"};
NSMutableArray *arr=[[NSMutableArray alloc]init];
[arr addobject:dic];
this is the way to get those values:
[arr objectforkeyValue #"kishore"];
NSMutableArray *dictArray = [[NSMutableArray alloc] init]; // created and initiated mutable array
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; // created and initiated mutable Dictionary
[dict setObject:#"object1" forKey:#"1"]; // added a key pair in dictionary (you can set multiple objects)
[dictArray addObject:dict]; // added dictionary to array (you can add multiple dictionary to array )
NSLog(#"dictionary inside an array : %#",dictArray[0][#"1"]); // access dictionary from array
You can try something like this
[NSArray arrayWithObjects:#{#"Image":#[#1,#2,#3]},#{#"Title":#[#1,#2,#3]},#{#"SubTitle":#[#1,#2,#3]}, nil];
You can easily create objects of array and access them back using following way.
NSArray *objimage=#[#"Test1.png",#"Test2.png",#"Test3.png"];
NSArray *objtitle=#[#"Test1",#"Test2",#"Test3"];
NSArray *objsubtitle=#[#"Test1",#"Test2",#"Test3"];
NSDictionary *obj_dictionary = #{#"image":objimage,#"Title":objtitle, #"subtitle":objsubtitle};
NSArray * obj_array= [[NSArray alloc] initWithObjects:obj_dictionary, nil]; // Create Array of nested objects
if([obj_array count] > 0) {
NSArray * imageArray = obj_array[0][#"image"]; // Access the nested objects in Array.
NSLog(#"%#", imageArray[0]);
}
first of all u need to declare correct variable name
NSArray *objImage=#[#"Test.png",#"Test.png",#"Test.png"];
NSArray *objTitle=#[#"Test",#"Test",#"Test"];
NSArray *objSubtitle=#[#"Test",#"Test",#"Test"];
at this point all u are created all the array, and u need to create dictionary like below
NSDictionary *obj_dictionary = #{#"image":objImage,#"title":objTitle, #"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[#"image"];
NSArray * obj_array = #[obj_dictionary]; //u can crate array of dictionary like this
in above obj_dictionary will contains all the array like below,
Title = (
Test,
Test,
Test
);
image = (
"Test.png",
"Test.png",
"Test.png"
);
subtitle = (
Test,
Test,
Test
);
and u can access the object in the dictionary like below using a key for example
NSArray * obj_array_images = obj_dictionary[#"image"];
gives an array of images that is associated with key image, similarly u can access other array like this by providing different keys associated with the dictionary obj_dictionary for example
NSArray * obj_array_titles = obj_dictionary[#"title"];
NSArray * obj_array_subtitles = obj_dictionary[#"subtitle"];
edit
NSArray *objImage=#[#"Test.png",#"Test.png",#"Test.png"];
NSArray *objTitle=#[#"Test",#"Test",#"Test"];
NSArray *objSubtitle=#[#"Test",#"Test",#"Test"];
NSDictionary *obj_dictionary = #{#"image":objImage,#"title":objTitle, #"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[#"image"];
NSArray * obj_array = #[obj_dictionary]; //u can crate array of dictionary like this

Resources