0

I have a UITextField with text: 9, 1, x, 1, 3, 3, y, 8

I need to remove all duplicates, remove all non-numeric characters and arrange the remaining numbers in ascending order.

So far I have:

-(IBAction)sortArrayPressed:(id)sender {

NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:self.textToParseTextField.text];

        NSLog([array description],nil);

}

The output reads:

Project[10176:2274463] (
    "9, 1, x, 1, 3, 3, y, 8"
)

Am I correct using NSMUtableArray? How do we handle parsing and sorting this sort of array which has both types Int and String? Thanks!

2
  • You need to parse the string. All you've done so far is add the single string as an element of the array. That doesn't do anything for you. Commented Mar 22, 2017 at 5:04
  • Is that one element in the array or just a mistake when making a quick example? Commented Mar 22, 2017 at 5:04

4 Answers 4

2

You can do like this, filter remove non-numeric string, convert to Set remove duplicate:

Obj-C:

NSString *str = @"9, 1, x, 1, 3, 3, y, 8";
NSArray* a = [str componentsSeparatedByString:@", "];
NSMutableArray *b = [NSMutableArray new];
for (NSString *digit in a) {
    NSRange range = [digit rangeOfString:@"[0-9]" options:NSRegularExpressionSearch];
    if (range.location == NSNotFound) {

    } else {
        [b addObject:digit];
    }
}
b = [NSMutableArray arrayWithArray:[NSSet setWithArray:b].allObjects];
[b sortUsingSelector:@selector(compare:)]; //(1,3,8,9)

Swift:

let a = "9, 1, x, 1, 3, 3, y, 8".components(separatedBy: ", ")
let b = a.filter({ $0.range(of: "[0-9]", options: .regularExpression, range: nil, locale: nil) != nil })
Array(Set(b)).sorted() //["1", "3", "8", "9"]
Sign up to request clarification or add additional context in comments.

1 Comment

The question is tagged Objective-C, the question's title mentions Objective-C, and the code in the question is in Objective-C. Posting an answer in Swift isn't helpful.
2

Use NSCharacterSet to get all number from string after that create array from it and then use NSSet to get the unique object. Sort your array from set and convert array to string.

NSString *string = @"9, 1, x, 1, 3, 3, y, 8";
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSArray *numberArray = [string componentsSeparatedByCharactersInSet:nonDigitCharacterSet];

NSMutableArray *soretedArray = [[NSMutableArray alloc] initWithArray:[[NSSet setWithArray:numberArray] sortedArrayUsingDescriptors:
                         @[[NSSortDescriptor sortDescriptorWithKey:@"integerValue"
                                                         ascending:YES]]]];
[soretedArray removeObject:@""];
NSString *newString = [soretedArray componentsJoinedByString:@", "];
NSLog(@"%@",newString); //1, 3, 8, 9

4 Comments

What if NSString *string = @"9, 1, x, 1, 3, 3, y7, 8"; and I want the Output as: 1, 3, 8, 9?
@nayem First of all that is not suggested by OP, but if you have string like that then you need to first separate you string to array and then need to check individually in for loop that that object is integer or not.
Well, I get it. I just asked it for my curiosity
@nayem I know you have asked for curiosity thats why I replied you, but I have not edited answer because OP still not replied here. If he want something like that will edit the answer.
0

Try this code :

 NSMutableArray *array1;
NSMutableArray *array2;
array1=[NSMutableArray arrayWithObjects:@"2",@"2",@"x",@"y",@"x",@"2",@"3",@"1",@"3",@"2",@"5",@"6",@"6",nil];//example
array2=[[NSMutableArray alloc]init];
for (id obj in array1)
{
    if (![array2 containsObject:obj])
    {
        [array2 addObject: obj];
    }
}
NSLog(@"new array is %@",array2);

Ascending order

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray* sortedArray = [array2 sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];                           NO;

NSLog(@"sortedArray==%@",sortedArray);

NSString * result = [sortedArray componentsJoinedByString:@""];

Remove non-numeric characters

NSCharacterSet *setToRemove =
[NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSCharacterSet *setToKeep = [setToRemove invertedSet];

NSString *newString =
[[result componentsSeparatedByCharactersInSet:setToKeep]
 componentsJoinedByString:@""];

NSLog(@"newString=====%@",newString);

Comments

0

remove duplicate from string

NSString *originalString = @"9, 1, x, 1, 3, 3, y, 8";
    NSMutableString *strippedString = [NSMutableString
                                       stringWithCapacity:originalString.length];

    NSScanner *scanner = [NSScanner scannerWithString:originalString];
    NSCharacterSet *numbers = [NSCharacterSet
                               characterSetWithCharactersInString:@"0123456789"];

    while ([scanner isAtEnd] == NO) {
        NSString *buffer;
        if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
            [strippedString appendString:buffer];

        } else {
            [scanner setScanLocation:([scanner scanLocation] + 1)];
        }
    }

    NSLog(@"%@", strippedString); 

   }

i hope this code will help u...

Comments

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.