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