iOS development--Several algorithms for sequential array de-weight

Source: Internet
Author: User

Algorithm, in the school when the grasp of the weak, and now still have to go.

NSMutableArray *dateMutablearray = [@[] mutableCopy];

NSArray *array1 = @[@"1",@"2",@"3", @"3",@"4",@"5",@"7",@"9",@"9", @"11",];NSMutableArray *array = [NSMutableArray arrayWithArray:array1];for(inti = 0; i < array.count; i ++) {    NSString *string = array[i];     NSMutableArray *tempArray = [@[] mutableCopy];    [tempArray addObject:string];    for(intj = i+1; j < array.count; j ++) {        NSString *jstring = array[j];        NSLog(@"jstring:%@",jstring);        if([string isEqualToString:jstring]) {           NSLog(@"jvalue = kvalue");           [tempArray addObject:jstring];        }    }    if([tempArray count] > 1) {        [array removeObjectsInArray:tempArray];        i -= 1;    //去除重复数据 新数组开始遍历位置不变    }}
Worst-of-the-nodups algorithm: de-heavy, unrelated to array whether public void () {//} iterates over for (int i=0; i<nelems-1; i++) {//with each subsequent comparison for (j=i+ 1; j<nelems;                    J + +) {//if equal, all subsequent entries move forward, total-1 if (A[i]==a[j]) {for (k=j+1; k<nelems; k++) {                    A[J] = a[j+1];                nelems--;    }}}}} put the following different forward public void nodups () {//array from small to large this.insertionsort ();    int i=0;    The first pointer int j=0;    The second pointer for (j=1; j<nelems;j++) {//encounters the same skip, the difference is added to the front if (A[j]!=a[i]) {A[++i]=a[j]} } nelems = i+1;}    Mark the duplicate as-1, assuming there is no negative public void nodups () {//array from large to small arrange this.insertionsort ();    Final int FLAG =-1; Repeating number marker int holdnumber; Number of numbers marked//Find duplicates and mark for (int i=0; i<nelems; i++) {for (int j=i+1; j<nelems; J + +) {if (a[i]==a [j])                {A[j] = FLAG;            holdnumber++; }}}//Handle tagged numbers//ideas and the first one as int i=0; Index bit    for (int j=1; j<nelems; J + +) {//The first cannot be a repetition//find the first mark bit if (A[j]==flag && i==0) {i        = J;            }else if (A[j]!=flag && i!=0) {//logic with the second algorithm a[i] = a[j];        i++; }} Nelems-= Holdnumber;}

iOS Development-several algorithms for sequential 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.