IOS Basics Nsarray Array de-weight

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.