简体   繁体   English

从自定义对象的NSArray中删除重复项,并对其进行比较和编辑

[英]Remove duplicates from NSArray of Custom Objects also compare and edit same

I have two NSMutableArrays . 我有两个NSMutableArrays Each contains a custom word object in it. 每个对象都包含一个自定义单词对象。 Custom word has 2 properties text and frequency. 自定义词有2个属性,文本和频率。 Now I want to combine these two arrays in such a way that, if these two arrays has same text in it, then it should compare the frequency of those two text and select the highest frequency of the two. 现在,我想以一种方式组合这两个arrays ,如果这两个数组中包含相同的文本,则它应该比较这两个文本的频率并选择两者的最高频率。 And also it should remove the duplicates from the array . 并且它也应该从array删除重复项。 I tried every logic for this but was not able to do this. 我为此尝试了所有逻辑,但无法做到这一点。 Can any body help me with the logic for this. 任何机构都可以在逻辑上帮助我。 Following the code. 遵循代码。 But it should also remove the duplicates. 但是它也应该删除重复项。

for (int i = 0; i < [array count]; i++) {
  for (int j = 0; j < [array count]; j++)  {
    if ([[[array objectAtIndex:i]firstWord] isEqualToString:[[array objectAtIndex:j] firstWord]]) {

      if ([[array objectAtIndex:i] frequency] < [[array objectAtIndex:j] frequency]) {
        CustomWordFrequency *word = [array objectAtIndex:i];
        word.frequency = [[array objectAtIndex:j] frequency];
        [array replaceObjectAtIndex:i withObject:word];
      }

    }
  }
}
  1. Combine the two arrays. 合并两个数组。
  2. Sort the resulting array by text ASC, frequency DESC 按文本ASC,频率DESC对结果数组进行排序
  3. Loop through the array 遍历数组
    a. 一种。 Look at the next item in the array. 查看数组中的下一项。 If the text is the same as the current word, remove it from the array. 如果文本与当前单词相同,则将其从数组中删除。 If it's different, continue looping. 如果不同,请继续循环。

     NSArray *combined = [firstArray arrayByAddingObjectsFromArray:secondArray]; [combined sortUsingComparator:^(id firstObject, id secondObject) { NSComparisonResult *result = [firstObject.text compare:secondObject.text]; if (result == NSOrderedAscending) return NSOrderedAscending; if (result == NSOrderedDescending) return NSOrderedDescending; if (result == NSOrderedSame) { result = [firstObject.frequency compare:secondObject.frequency]; if (result == NSOrderedAscending) return NSOrderedDescending; if (result == NSOrderedDescending) return NSOrderedAscending; if (result == NSOrderedSame) return NSOrderedSame; } }]; for (int i = 0; i < [combined count] - 2; ++i) { CustomWordFrequency *word = [combined objectAtIndex:i]; int j = i + 1; while ([word.text compare:[combined objectAtIndex:j].text == NSOrderedSame) { [combined removeObjectAtIndex:j]; j++; if (j == [combined count]) {break;} } if (i >= [combined count] - 2) {break;} // the count keeps changing so check here } 
NSMutableArray *combinedArray = [[NSMutableArray alloc] init];
BOOL flagForMatchFound = FALSE;
for(CustomWordFrequency *firstWord in firstArray)
 {
   flagForMatchFound = FALSE;
   for(CustomWordFrequency *secondWord in secondArray)
   {
       if([firstWord.firstWord isEqualToString:secondWord.firstWord])
       {
          if(firstWord.frequency >= secondWord.frequency)
          {
               [combinedArray addObject:firstWord];
               flagForMatchFound = TRUE;
          }
          else
               [combinedArray addObject:secondWord];
       }
       else
       {
          if(!flagForMatchFound)
              [combinedArray addObject:secondWord]; 
       }
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM