Summary of the method of array de-weight

Source: Internet
Author: User

These days are sorting out the interview questions, by the way to summarize the previous lessons, the array to heavy, I guess the interview should test the odds are pretty high, so let the blogger to tell the iOS array to the weight of several methods.

  • The first kind: bubble sort, and then each to compare, this nothing to say, the key is trouble, and time complexity is also very large, so bloggers do not tell.
  • The second type: Sort by array.
  • Nsarray *array = @[@"Xiao Yu",@"Little Fish",@"Xiao Yu"]; //---Method of array de-weight method 1//Create a new mutable array with an array length of 0Nsmutablearray *newarray =[Nsmutablearray array]; //iterates through the array and then determines whether the new array contains the element, or if not, adds it.      for(NSString *string inchArray) {                if(! [NewArray Containsobject:string]) {[NewArray addobject:string]; }    }    //output elements in a new array     for(NSString *string inchNewArray) {NSLog (@"%@",string); }
  • The Third Kind: dictionary sort
  •     Nsarray *array = @[@ "Xiao Yu", @ "small fish", @ "Little Jade"];
     //  string de-weight method two  nsmutabledictionary *    Dict = [Nsmutabledictionary dictionary];  //  write the array  for  (nsnumber *number in   array) {//  stitch number into string  [Dict setvalue:number forkey:[nsstring stringwithformat:@ " %@      ,number]]; } NSLog ( @ " %@   ", [Dict allvalues]); 
  • Fourth kind: nsset sort-according to Nsset is disorderly, he can guarantee the uniqueness of the data, inserting the same data will not have any effect, Bo Master suggested that everyone can interview with Nsset, convenient, not lazy programmers are not good programmers.
Nsarray *array = @[@ "Little Jade "@ "Little fish "@" little Jade "  ];        // The third method of Nsset    the weight Nsset *Set = [[Nsset Alloc]initwitharray:array];        NSLog (@ "%@", [set allobjects]);

    • Nsorderedset--orderly arrangement--interview if the order is ordered to use this, no need to use the above Nsset
    Nsarray *array = @[@ " Little Jade "@ "Little fish "@" Little Jade " "];     // Array Order    Nsorderedset *orderset = [Nsorderedset Orderedsetwitharray:array];    NSLog (@ "%@", Orderset.array);

Summary of the method of 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.