5

I am working on SQLite and I have written a query which returns me two arrays ItemsArray and CustomersIDArray as:

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

I want result like that Off White = 2 (count) , Fan = 2 (count) and Delux = 1; and the Resultant Array,

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

Actually I want the count of repetition in first array but the value must not same for CustomerArray. Please help me through logic or code.

4
  • Try solution provided on this question: (stackoverflow.com/questions/6841072/…) Commented Apr 23, 2012 at 12:39
  • @INoob Dear I don't know how to use NSCountedSet.. Can you please tell me ? Commented Apr 23, 2012 at 12:45
  • Try the answer here: stackoverflow.com/a/7606138/876283 Commented Apr 23, 2012 at 12:50
  • @iNoob how to count the repeated elements count :( Commented Apr 23, 2012 at 12:59

3 Answers 3

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);
}

I just set up -getArrayInfo to test it out. Works fine. As you can see, the array you want to display will be at index:0, and the countArray at index:1.

Sign up to request clarification or add additional context in comments.

4 Comments

its really nice work I commit some changes according to me... Thumbs up :)
I have another problem can you help me?
Sure-- post a question, and leave a link to it as a comment here.
the above answser is logically incorrect. try with this input NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"Off White", @"Fan", @"Fan", @"Off White", @"Deluxe", @"Fan", @"Fan", nil]; . you can see count of "fan" is 3 but actually it is four. To correct this issue you need to decrement j after an element is removed from array (otherwise the next string in the array is not evaluated). edited the answer
2

Use NSCountedSet like below

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);

2 Comments

I need result in Array if two time then save 2 again that item if three the save 3 if one then 1.
Yes ... but also want to know how many times it comes in the Array
0

try this:

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--;
}

2 Comments

I Change it and its works for me. its update Items Array. But i also want Count of Repeated items.
I suggest not to use the name copy to the array as people can get confused with the message copy you also used in: NSArray *copy = [ItemsArray copy];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.