iOS array de-weight, empty, Delete elements, Delete duplicate elements, etc.

Source: Internet
Author: User
<span id="Label3"></p><p><p>One: Go Heavy</p></p><p><p>Sometimes it is necessary to remove duplicate elements from the nsarray, and the presence of elements in the nsarray is not necessarily the NSString type. Today I think about it, add friends help, think of two solutions, first divided into the Following.</p></p>1. Use the Nsdictionary AllKeys (allvalues) method to store the elements in the Nsarray in a dictionary, and then use AllKeys or allvalues to get all the keys or values of the dictionary, all of which are de-weighed. Example Code:<p><p>Nsarray *arr = @[@111,@222,@111];</p></p><p><p>Nsmutabledictionary *dict = [nsmutabledictionary dictionary];</p></p><p><p>For (nsnumber *number in Arr) {</p></p><p><p>[dict Setobject:number forkey:number];</p></p><p><p>}</p></p><p><p>NSLog (@ "%@", [dict allvalues]);</p></p><p><p>The output is:</p></p><p><p></p></p><p><p>2013-05-21 12:03:49.449 test1[4377:c07] (</p></p><p><p>111,</p></p><p><p>222</p></p><p><p>)</p></p>2. This method is faster with the Nsset allobjects method, and the Nsset does not add the properties of the repeating Element. But the go-to-heavy array is not sorted, and if you need to sort, you can use the Nssortdescriptor class. Example Code:<p><p>Nsarray *arr = @[@111,@222,@111];</p></p><p><p>Nsset *set = [nsset setwitharray:arr];</p></p><p><p>NSLog (@ "%@", [set allobjects]);</p></p><p><p>The output is:</p></p><p><p></p></p><p><p>2013-05-21 12:06:26.508 test1[4547:c07] (</p></p><p><p>111,</p></p><p><p>222</p></p><p><p>)</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>Sort of the De-weight</strong></p></p><p><p></p></p><p><p>Many times you may encounter elements that need to be removed from nsarray, using a number of METHODS.</p></p><p><p></p></p><p><p>The most convenient and fast is the nsset. but nsset, sometimes the order of the array elements</p></p><p><p></p></p><p><p>Upset. Then another method is as Follows:</p></p><p><p></p></p><p><p></p></p><p><p>Nsmutablearray *categoryarray = [[nsmutablearray alloc] init];</p></p><p><p>for (unsigned i = 0; i < [namearray count]; I++) {</p></p><p><p>If ([categoryarray containsobject:[namearray objectatindex:i]] = = NO) {</p></p><p><p>[categoryarray Addobject:[namearray objectatindex:i];</p></p><p><p>}</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>The order will not be chaotic.</p></p><p><p>Some developers, when judging whether the array is empty, will be written as:</p></p><p><p></p></p><strong><strong>[objc]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span> <ol class="dp-objc" start="1"> <ol class="dp-objc" start="1"> <li class="alt"><span class="keyword">If (array! =<span class="keyword"> Nil)</span></span></li> </ol> </ol><p><p><br>Or:</p></p><p><p></p></p><strong><strong>[objc]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span> <ol class="dp-objc" start="1"> <ol class="dp-objc" start="1"> <li class="alt"><span class="keyword"><span class="keyword"><span class="keyword">if (array<span class="xcodeconstants">. count! = <span class="xcodenumber">0) three: Delete element</span> </span> </span></span></span><p><p>This problem can be seen in two ways: one is to delete the loop when it is traversed, and the other is to operate after the loop has been traversed.</p></p><p><p>A "in the loop traversal when the operation, there are several methods:</p></p><p><p>1. To end with a break after using a quick traverse for the delete operation, so there is no problem.</p></p><p><p></p></p><p><p>Nsmutablearray *arr1 = [[nsmutablearrayalloc]initwithobjects:@ "abc", @ "DEF", @ "abc", @ "abc", @ "qwe", @ "TTT", nil];</p></p><p><p></p></p><p><p>For (nsstring *strin Arr1)</p></p><p><p>{</p></p><p><p>NSLog (@ "%@", str);</p></p><p><p>If ([str isequaltostring:@ "ABC"])</p></p><p><p>{</p></p><p><p>[ARR1 removeobject:str];</p></p><p><p>Break</p></p><p><p>}</p></p><p><p>}</p></p><span class="keyword"><span class="keyword"><span class="keyword"><span class="xcodeconstants"><span class="xcodenumber"><br></span></span></span></span></span><p><p>2. Use the index count method, but note that when we delete an element, Arr.count is changed, so it may cause the omission of the deletion, so we have to operate on the index count, every time we delete an element, let i-1, so as to achieve the effect of no heavy leakage</p></p><p><p></p></p><p><p>Nsmutablearray *arr1 = [[nsmutablearrayalloc]initwithobjects:@ "abc", @ "DEF", @ "abc", @ "abc", @ "qwe", @ "TTT", nil];</p></p><p><p></p></p><p><p>For (int i =0;i<arr1.count;i++)</p></p><p><p>{</p></p><p><p>NSLog (@ "%lu", arr1.count);</p></p><p><p>NSString *str = arr1[i];</p></p><p><p>If ([str isequaltostring:@ "ABC"])</p></p><p><p>{</p></p><p><p>[ARR1 removeobjectatindex:i];</p></p><p><p>i--;</p></p><p><p>}</p></p><p><p></p></p><p><p>}</p></p><p><p>For (nsstring *strin Arr1)</p></p><p><p>{</p></p><p><p>NSLog (@ "%@", str);</p></p><p><p>}</p></p>3. It is also a way to use reference counting, but in reverse order to delete, so that even after the subsequent element is deleted, it will not affect the previous element in the array index<p><p>Nsmutablearray *arr1 = [[nsmutablearrayalloc]initwithobjects:@ "abc", @ "DEF", @ "abc", @ "abc", @ "qwe", @ "TTT", nil];</p></p><p><p></p></p><p><p>for (int i = (int) arr1.count-1; i>=0; i--)</p></p><p><p>{</p></p><p><p>NSLog (@ "%lu", arr1.count);</p></p><p><p>NSString *str = arr1[i];</p></p><p><p>If ([str isequaltostring:@ "ABC"])</p></p><p><p>{</p></p><p><p>[ARR1 removeobjectatindex:i];</p></p><p><p>}</p></p><p><p></p></p><p><p>}</p></p><p><p>For (nsstring *strin Arr1)</p></p><p><p>{</p></p><p><p>NSLog (@ "%@", str);</p></p><p><p>}</p></p><p><p></p></p><p><p>4. Using the system method, Enumerateobjectsusingblock inside has a stop pointer, when we find the corresponding str, so that *stop = YES.</p></p><p><p>[ARR1 enumerateobjectsusingblock:^ (id _nonnull obj, nsuinteger idx, BOOL * _nonnull stop) {</p></p><p><p>NSLog (@ "%lu", idx);</p></p><p><p>If ([obj isequaltostring:@ "ABC"])</p></p><p><p>{</p></p><p><p>*stop = YES;</p></p><p><p>[ARR1 removeobject:obj];</p></p><p><p>}</p></p><p><p>}];</p></p><p><p></p></p><p><p>Two "in the loop after the completion of the operation, the main way is to traverse to find the corresponding index, or to add an identifier to him, and finally to unify the Deletion.</p></p><p><p>1. The string to be deleted is placed in an array, and the last one is deleted</p></p><p><p></p></p><p><p>Nsmutablearray *arr1 = [[nsmutablearrayalloc]initwithobjects:@ "QQQ", @ "abc", @ "DEF", @ "abc", @ "abc", @ "qwe", @ "TTT", Nil ];</p></p><p><p>Nsmutablearray *arr = [[nsmutablearrayalloc]init];</p></p><p><p>For (nsstring *strin Arr1)</p></p><p><p>{</p></p><p><p>If ([str isequaltostring:@ "ABC"])</p></p><p><p>{</p></p><p><p>[arr addobject:str];</p></p><p><p>Break If you delete only one string, you can end it when you find it, and you don't have to look for it, so if you want to delete more than one string, you should find all the strings to be deleted</p></p><p><p>}</p></p><p><p>}</p></p><p><p>[ARR1 removeobjectsinarray:arr];</p></p><br><p><p>2. The following table containing the string to be deleted is added in a nsmutableindexset, and the last one is deleted</p></p><p><p></p></p><p><p>Nsmutablearray *arr1 = [[nsmutablearrayalloc]initwithobjects:@ "QQQ", @ "abc", @ "DEF", @ "abc", @ "abc", @ "qwe", @ "TTT", Nil ];</p></p><p><p></p></p><p><p>Nsmutableindexset *set = [[nsmutableindexsetalloc]init];</p></p><p><p>For (int i =0; i < arr1.count; i++)</p></p><p><p>{</p></p><p><p>NSString *str = arr1[i];</p></p><p><p>If ([str isequaltostring:@ "ABC"])</p></p><p><p>{</p></p><p><p>[set addindex:i];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>[ARR1 removeobjectsatindexes:set];</p></p><p><p></p></p><p><p>Four: Delete duplicate elements</p></p><p><p>Nsmutablearray *result = [nsmutablearray array];<br>For (nsstring *string in Arr) {<br>If (![ Result Containsobject:string]) {<br>[result addobject:string];<br>}<br>}</p></p><p><p></p></p><p><p></p></p><p><p></p></p></li> </ol> </ol><p><p>iOS array de-weight, empty, Delete elements, Delete duplicate elements, etc.</p></p></span>

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.