#include <Foundation/Foundation.h>


// .h
@interface Solution : NSObject 
+ (NSArray*) removeDuplicate:(NSArray*) nums;
@end

// .m
@interface Solution ()
@end

@implementation Solution 

+ (NSArray*) removeDuplicate:(NSArray*) nums {

    if (nums == nil) {
        return nil;
    }

    NSMutableArray* res = [NSMutableArray array];
    NSMutableSet* dict = [NSMutableSet set];

    for (id num in nums) {
        if (![dict containsObject: num]) {
            [res addObject: num];
            [dict addObject: num];
        }
    }
    return res;
}
@end


int main(void)
{

    //NSArray* nums = [NSArray arrayWithObjects: @1, @2, @4, nil];
    NSArray* nums = @[@2, @1, @3, @1, @2];
    NSLog(@"%@", [Solution removeDuplicate: nums]);
    return 0;
}

Given an array, remove the duplicates and return a unique array keeping the first

occurrence of the duplicates and the order. [@2, @1, @3, @1, @2] --> [@2, @1,

@3]

results matching ""

    No results matching ""