简体   繁体   中英

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

I have two NSMutableArrays . Each contains a custom word object in it. Custom word has 2 properties text and frequency. 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. And also it should remove the duplicates from the 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
  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]; 
       }
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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