1. Interview questions
Now there is an array arr1, it stores the strings are @ "Zhangsan" @ "Lisi" @ "Wangwu" @ "Lisi" @ "Zhangsan", please assign it to the variable array arr2 output as: @ "Zhangsna", @ "Lisi" , @ "Wangwu".
Problem Solving Ideas:
1) Create a mutable dictionary
2) Iterate over this array to store the string of the array as the key and value of this dictionary
3) Call the dictionary any method
@property (readonly, copy) nsarray<keytype> *allkeys;
@property (readonly, copy) nsarray<objecttype> *allvalues;
4) The obtained string is stored in the ARR2.
The code is as follows:
1Nsarray *arr1 = @[@"Zhangsan",@"Lisi",@"Wangwu",@"Lisi",@"Zhangsan"];2 3Nsmutabledictionary *dict =[Nsmutabledictionary dictionary];4 for(NSString * STRincharr1) {5 [Dict setobject:str forkey:str];6 }7 8Nsmutablearray * arr2 =[Nsmutablearray arraywithobjects:[dict AllKeys], nil];9 TenNSLog (@"%@", ARR2);
Output Result:
$-32.058-nsarray[1829: 263154 ] ( ( Zhangsan, Lisi, Wangwu ))
Principle: This is based on the characteristics of the dictionary: The key value is unique, when the two Zhangsan, he will not create a key value pair, but to the previous Zhangsan key value pair and re-assign value
eg
Nsmutabledictionary *dict = [[Nsmutabledictionary alloc]initwithobjects:@[@ "zhangsan", @" Lisi "] forkeys:@[@ "1"@ "1"]; NSLog (@ "%@", dict);
Output Result:
2016-03-21 18:09:54.571 01-nsarray[1865:265560] { 1 = lisi;}
IOS Basics Nsarray Array de-weight