2012-04-23 10 views
5

sto lavorando su SQLite e ho scritto una domanda che mi restituisce due matrici ItemsArray e CustomersIDArray come:Come trovare valori duplicati nelle matrici?

ItemsArray 
Element at Index 0 = Off White, 
Element at Index 1 = Fan, 
Element at Index 2 = Off White, 
Element at Index 3 = Delux, 
Element at Index 4 = Fan 

CustomerIDArray 
Element at Index 0 = 1, 
Element at Index 1 = 2, 
Element at Index 2 = 2, 
Element at Index 3 = 3, 
Element at Index 4 = 4 

voglio risultare come quello Bianco = 2 (contare), Fan = 2 (contare) e Delux = 1; e la matrice risultante,

Result Array 
Element at Index 0 = Off White, 
Element at Index 1 = Fan, 
Element at Index 2 = Delux 

realtà voglio il conteggio di ripetizione nella prima matrice ma il valore non deve stesso per CustomerArray. Aiutatemi tramite la logica o il codice.

+0

Provare la soluzione fornita su questa domanda: (http://stackoverflow.com/questions/6841072/how-to-count-duplicates-values-in-nsarray) – iNoob

+0

@INoob Caro, non so come usare NSCountedSet .. Potete dirmi, per favore ? –

+0

Prova la risposta qui: http://stackoverflow.com/a/7606138/876283 – iNoob

risposta

3
-(NSMutableArray *)getCountAndRemoveMultiples:(NSMutableArray *)array{ 

    NSMutableArray *newArray = [[NSMutableArray alloc]initWithArray:(NSArray *)array]; 
    NSMutableArray *countArray = [NSMutableArray new]; 
    int countInt = 1; 
    for (int i = 0; i < newArray.count; ++i) { 
     NSString *string = [newArray objectAtIndex:i]; 
     for (int j = i+1; j < newArray.count; ++j) { 
      if ([string isEqualToString:[newArray objectAtIndex:j]]) { 
       [newArray removeObjectAtIndex:j]; 
       countInt++; 
      } 
     } 
     [countArray addObject:[NSNumber numberWithInt:countInt]]; 
     countInt = 1; 
    } 
    NSMutableArray *finalArray = [[NSMutableArray alloc] initWithObjects:newArray, countArray, nil]; 
    NSLog(@"%@", finalArray); 
    return finalArray; 

} 
- (IBAction)getArrayInfo:(id)sender { 
    NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"Off White", @"Fan", @"Off White", @"Deluxe", @"Fan", nil]; 
    NSMutableArray *godArray = [self getCountAndRemoveMultiples:myArray]; 
    NSLog(@"Array from this end = %@", godArray); 
} 

ho appena impostato fino -getArrayInfo di provarlo. Funziona bene. Come puoi vedere, la matrice che vuoi visualizzare sarà all'indice: 0 e al countArray all'indice: 1.

+0

la sua davvero un bel lavoro ho commesso alcuni cambiamenti secondo me ... Thumbs up :) –

+0

ho un altro problema può aiutarmi? –

+0

Certo-- pubblica una domanda e lascia un link ad essa come commento qui. – AMayes

0

provare questo:

NSArray *copy = [ItemsArray copy]; 

NSInteger index = [copy count] - 1; 

for (id object in [copy reverseObjectEnumerator]) { 

    if ([ItemsArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) { 
     [ItemsArray removeObjectAtIndex:index]; 
    } 
    index--; 
} 
+0

Sto usando NSMutableArray che funzionerà anche per questo? –

+0

ne dite di provare con un piccolo cambiamento per il codice che ho postato sopra ... – chewy

+0

lo cambio e le sue opere per me. la sua aggiornare gli elementi di array. Ma voglio anche il conte di elementi ripetuti. –

1

Usa NSCountedSet come qui di seguito

NSMutableArray *ary_res = [[NSMutableArray alloc] init]; 
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"11",@"13",@"34",@"9",@"13",@"34",@"9",@"2",nil]; 
    NSCountedSet *set = [[NSCountedSet alloc] initWithArray:array]; 
    for(id name in set) 
    { 
     if([set countForObject:name]==2) 
      [ary_res addObject:name]; 
    } 
    // 
    NSLog(@"%@",ary_res); 
+0

ho bisogno di provocare Array se due di tempo quindi salvare nuovamente 2 quell'elemento se tre il salvataggio 3 se uno poi 1. –

+0

@AmirIphone significa che se si "Off White" 3 volte poi di quanto si desidera una volta ... – Narayana

+0

Sì ... bu t vuole anche sapere quante volte si tratta in Array –

Problemi correlati