2013年5月7日 星期二

取得出現頻率最高資料

mark 一下,晚點看

來源 http://d.hatena.ne.jp/tanaponchikidun/searchdiary?word=%2A%5Bobjective-c%5D

// NSNumber配列を初期化

NSArray *numbers = @[@9,@4,@5,@1,@1,@17,@9,@6,@9];

// 昇順ソート

NSArray *asecNumbers = [numbers sortedArrayUsingSelector:@selector(compare:)];

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

// 降順ソート

NSSortDescriptor *descDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];

NSArray *descNumbers = [numbers sortedArrayUsingDescriptors:@[descDescriptor]];

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

// 最大、最小、平均、合計値の取得

NSLog(@"max of numbers = %@",[numbers valueForKeyPath:@"@max.self"]);

NSLog(@"min of numbers = %@",[numbers valueForKeyPath:@"@min.self"]);

NSLog(@"avg of numbers = %@",[numbers valueForKeyPath:@"@avg.self"]);

NSLog(@"sum of numbers = %@",[numbers valueForKeyPath:@"@sum.self"]);

// 重複データを取り除く

NSArray *distinctNumbers = [numbers valueForKeyPath:@"@distinctUnionOfObjects.self"];

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

// 6以上のデータを取得

NSPredicate *over6Pred = [NSPredicate predicateWithFormat:@"SELF >= 6"];

NSArray *over6Array = [numbers filteredArrayUsingPredicate:over6Pred];

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

// 最頻出のデータを取得

NSCountedSet *set = [NSCountedSet setWithArray:numbers];

int highest = 0;

NSNumber *mostFqtNumber = @0;

for (NSNumber *n in set) {

if ([set countForObject:n] > highest) {

highest = [set countForObject:n];

mostFqtNumber = n;

}

}

NSLog(@"most frequent number = %@",mostFqtNumber);

沒有留言: