-1

How to check NSArray objectAtIndex that the value in it does not repeat?

I have have two integer A and B. Both giving random values. Now I have int C also, C = B - A; I want the numbers does not repeat after subtraction.

Means if the outcome comes after subtraction 2 then two will not come again. If 2 come again then I want some calculation and generate new numbers again so that will give another value instead of 2.

I am not able to do this yet.

I tried NSMutableArray and stored the value in it but and compare the previous value could not succeeded.

This is what I am doing

int yourInt = C;

[chk_Array addObject:[NSNumber numberWithInt:yourInt]];

int var_Count_chk_Array;
var_Count_chk_Array = [chk_Array count];
NSLog(@"var_Count_chk_Array is: %d",var_Count_chk_Array);

NSLog(@" check array : %@",chk_Array);



for (id anObject in chk_Array) {
    if (anObject == [NSNumber numberWithInt:0]) {
        // Do something
         NSLog(@"Catch ");
    }
else {

A = arc4random()% 8;
NSLog(@"A is: %d",A);

B = arc4random()% 11;
NSLog(@"B is: %d",B);

C = B - A;
NSLog(@"C is: %d",C);


lbl_A.text = [NSString stringWithFormat:@"%i",A];


if (A>B) {

    NSLog(@"A is: %d",A);
    NSLog(@"B is: %d",B);

    B = B + A +  5;

    NSLog(@"A is: %d",A);
    NSLog(@"B is: %d",B);

    lbl_B.text = [NSString stringWithFormat:@"%i",B];


    C = 0;

    C = B - A;
    NSLog(@"C is: %d",C);

    lbl_C.text = [NSString stringWithFormat:@"%i",C];
}
else{

    lbl_B.text = [NSString stringWithFormat:@"%i",B];

    NSLog(@"C is: %d",C);
    lbl_C.text = [NSString stringWithFormat:@"%i",C];
}


}

I just want that the result after subtraction comes different from previous once. digit not come again.

Any Idea or suggestions would be highly welcome.

2
  • using [chk_Array containsObject:[NSNumber number WithInt:yourValue]] may help you. Jul 16, 2013 at 12:06
  • 3
    Sad to say that this is a duplicate. The question recurs about twice a week.
    – Hot Licks
    Jul 16, 2013 at 12:09

2 Answers 2

1

check like this,

 NSArray *USAValues = [[NSArray alloc]initWithObjects:@"1",@"2",@"1", nil];

    if([USAValues containsObject:@"9"]){
        NSLog(@"already exist");
    }
    else{
        NSLog(@"new Value");

    }

EDIT:

   if([youArray containsObject:newlyGenratedValue]){
        NSLog(@"already exist");
    }
    else{
        NSLog(@"new Value");

    }
0

You can use predicate and it will work fine.

Alternate way is to use NSSet instead of NSArray. Each time you add an object in NSSet, it's count must increase. As Per definition of Sets, it can not hold duplicate values. So duplicate values will not reflect any change in Set.

For Example : Set is having {22.34.15} and you add one more value "15". Final set will be {22.34.15}, so no change in set. observer such cases where count is not increasing, redo the substraction.

Hope it helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.