首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS NSMutableArray 統計元素出現次數:使用 NSCountedSet 高效實現

     編輯:程式設計綜合問答

在 iOS 中統計數組中各元素出現次數,使用 NSCountedSet(計數集合)是最高效、代碼最簡潔的方案,避免手動遍歷字典計數。

問題場景

NSMutableArray *array = [@[@1, @2, @3, @3, @2, @1, @6, @2] mutableCopy];
// 期望輸出:1=2, 2=3, 3=2, 6=1

標準解決方案:NSCountedSet

// 1. 創建計數集合
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];

// 2. 遍歷輸出統計結果
for (NSNumber *number in countedSet) {
    NSUInteger count = [countedSet countForObject:number];
    NSLog(@"%@ = %lu", number, (unsigned long)count);
}

// 輸出:
// 1 = 2
// 2 = 3
// 3 = 2
// 6 = 1

NSCountedSet 核心特性

  • 繼承自 NSMutableSet,元素唯一(集合特性)
  • 內部維護每個元素的計數器(袋/多重集合特性)
  • countForObject: 返回元素添加次數
  • addObject: 增加計數,removeObject: 減少計數

手動字典計數對比(不推薦)

// 低效手動實現
NSMutableDictionary *countDict = [NSMutableDictionary dictionary];
for (id obj in array) {
    NSNumber *count = countDict[obj];
    countDict[obj] = @(count.unsignedIntegerValue + 1);
}
// 代碼冗長、易出錯、性能略低

進階:獲取出現次數最多的元素 (Top K)

- (NSArray *)topKFrequentElements:(NSArray *)array k:(NSUInteger)k {
    NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
    
    // 轉換為字典排序
    NSMutableDictionary *freqDict = [NSMutableDictionary dictionary];
    for (id obj in countedSet) {
        freqDict[obj] = @([countedSet countForObject:obj]);
    }
    
    // 按計數降序排序
    NSArray *sortedKeys = [freqDict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj2 compare:obj1]; // 降序
    }];
    
    // 取前 K 個
    NSUInteger limit = MIN(k, sortedKeys.count);
    return [sortedKeys subarrayWithRange:NSMakeRange(0, limit)];
}

適用類型說明

類型是否支援說明
NSNumber / NSString✅符合 NSCopying 協議
自定義對象✅ (需實現)需實現 isEqual: 和 hash,並符合 NSCopying
基本數據類型 (int, float)❌需包裝為 NSNumber

Swift 版本

let array = [1, 2, 3, 3, 2, 1, 6, 2]
let countedSet = NSCountedSet(array: array)

for obj in countedSet {
    let count = countedSet.count(for: obj)
    print("\(obj) = \(count)")
}

// 或使用 Swift 原生
let frequencies = Dictionary(array.map { ($0, 1) }, uniquingKeysWith: +)
// frequencies: [2: 3, 3: 2, 1: 2, 6: 1]
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved