0

There is an array in my app having multiple same values in it. I need to delete only one value at a time from array whether it has same more values in it.

Level1 Business, 
Level2 Economy, 
Level2 Economy,
Level1 Business

How this can be achieved, and main thing is that these values are dynamic these can be more or less also. Please guide for above. Below is what i tried.

if([arr containsObject:[NSString stringWithFormat:@"%d",ind]]){ 
[arr removeObject:[NSString stringWithFormat:@"%d",ind]]; 
}

This thing removes all similar entries, not required. Thanks in advance.

4
  • Why don't you get distinct values out of array, rather than deleting one by one ? Commented Jun 7, 2013 at 13:18
  • @RajanBalana These are type of seats in seat map and multiple seats have same type and level. Thus when deselecting a seat removes the entry from array of its type and level. Commented Jun 7, 2013 at 13:19
  • wouldn't it be much easier to keep a dictionary instead where you keep the count for each different element that you can decrement?
    – micantox
    Commented Jun 7, 2013 at 13:21
  • I have added Code for you.Check my Answer : stackoverflow.com/a/16985413/1603072
    – Bhavin
    Commented Jun 7, 2013 at 13:35

7 Answers 7

4

try like this,

NSArray *array = [NSArray arrayWithObjects:@"Level1 Business", @"Level2 Economy", @"Level2 Economy", @"Level1 Business", nil];
NSMutableArray *mainarray=[[NSMutableArray alloc]initWithArray:array];
int n=[mainarray indexOfObject:@"Level2 Economy"];//it gives first occurence of the object in that array
if(n<[mainarray count]) // if the object not exist then it gives garbage value that's why here we have to take some condition
    [mainarray removeObjectAtIndex:n];
NSLog(@"%@",mainarray);

O/P:-

(
    "Level1 Business",
    "Level2 Economy",
    "Level1 Business"
)
4
  • are you waiting for me to ask new. question Commented Jun 7, 2013 at 13:24
  • anyways thanks it seems correct and working also, but i will accept it on monday after properly going through it. Thanks for answer. Commented Jun 7, 2013 at 13:27
  • this will only remove the first occurrence of that item.... what if you had 3 array slots with the same value ? this would only remove one of them. Commented Jun 7, 2013 at 13:46
  • NSMutableArray* newLessonedArray = [[NSMutableArray alloc] init]; for ( NSString* str in mainArray ) { if ( ![newLessonedArray containsObject:str] ) { [newLessonedArray addObject:str]; } } this will work as long as you are only using strings.... Commented Jun 7, 2013 at 14:08
1

As you say,

[array removeObject:@"SomeObject"];

removes all instances of where isEqual: returns YES. To remove only the first instance, you can use something like

NSUInteger index = [array indexOfObject:@"SomeObject"];
if(index != NSNotFound) {
    [array removeObjectAtIndex:index];
}
1

Use [arr removeObjectAtIndex:yourIndex ] to remove your object at perticular postion at dynamic

1

Sample Code :

NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
NSUInteger obj = [arr indexOfObject:@"hi"];  //Returns the lowest integer of the specified object
[arr removeObjectAtIndex:obj];  //removes the object from the array
NSLog(@"%@",arr);

In your Case :

if([arr containsObject:[NSString stringWithFormat:@"%d",ind]])
{ 
     NSUInteger obj = [arr indexOfObject:[NSString stringWithFormat:@"%d",ind]];  //Returns the lowest integer of the specified object
     [arr removeObjectAtIndex:obj];
}
0

Here your requirement is like definition of NSSet, which contains unique objects only. But this will implies only if both the same value objects, are really in referring to same memory location as well.

If this is the case then and then, you can try code mentioned below:

// create set from an array
NSSet *telephoneSet = [NSSet setWithArray: myArray];

// create array from a set
NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];

I don't know whether it will work for your requirement or not. But for that, it would be required to check the object equality level.

Still it might help you as an less line of code.

0
NSMutableArray *uniques= [[NSMutableArray alloc] init];

for (NSString *word in duplicateWordsArray){
    if (!uniques.contains(word)){
            [ uniques addObject:word];
    }
}

I wrote this from my phone so it isn't formatted for code, but this will do it for you quickly and you'll have an array (uniquearray) that has unique words. Then you can use that one or set your original array = to unique array

0
NSArray *input = [NSArray arrayWithObjects:@"Level1 Business", @"Level2 Economy", @"Level2 Economy", @"Level1 Business", nil];
    NSMutableArray *output = [[NSMutableArray alloc] init];
    [output addObject:[input objectAtIndex:0]];
    for(NSString *value in input) {
      if(![output containsObject:value]) 
        [output addObject:value];
    }

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.