当前位置:网站首页>Illuminate\Support\Collection 去重 unique 列表去重
Illuminate\Support\Collection 去重 unique 列表去重
2022-06-23 04:53:00 【fareast_mzh】
列表, 数组 指定规则去重
curMsg->getFundId() == $prevMsg->getFundId()
重复后处理
// 删除哪个元素?
if ($curMsg->created_at > $prevMsg->created_at) {
// 待删除的元素移动到后面
self::swap($list, $i, $dupIdx);
}
可替换部分如上
没有排序,就这样吧
private static function swap(Collection $c, int $i, int $j) {
$tmp = $c->get($i);
$c->offsetSet($i, $c->get($j));
$c->offsetSet($j, $tmp);
}
/**
* 去除fund_id重复, 相同fund_id取最新的记录,删掉旧的记录
* @param Collection $list
*/
private static function msgListUniq(Collection $list) {
$n = $list->count();
$countDel = 0;
for ($i = 1; $i < $n; $i++) {
$dupIdx = -1;
/** @var $curMsg PayMessage */
$curMsg = $list->get($i);
if (empty($curMsg)) {
break;
}
$prevMsg = null;
for ($j = 0; $j < $i; $j++) {
/** @var $prevMsg PayMessage */
$prevMsg = $list->get($j);
if ($curMsg->getFundId() == $prevMsg->getFundId()) {
$dupIdx = $j;
break;
}
}
// 有重复的fund_id
if (0 <= $dupIdx) {
if ($curMsg->created_at > $prevMsg->created_at) {
self::swap($list, $i, $dupIdx);
}
for ($k = $i+1; $k < $n-$countDel; $k++) {
$list->offsetSet($k-1, $list->get($k));
}
// $list->offsetUnset($k-1);
$countDel += 1;
}
}
// 不要循环中unset
while ($countDel > 0) {
$list->offsetUnset($n - $countDel);
$countDel -= 1;
}
}边栏推荐
- SQL表名与函数名相同导致SQL语句错误。
- Leetcode topic resolution remove nth node from end of list
- Pat class B 1024 scientific notation C language
- Leetcode topic resolution valid Sudoku
- [focus on growth and build a dream for the future] - TDP year-end event, three chapters go to the Spring Festival!
- mysql读已提交和可重复度区别
- WordPress aawp 3.16 cross site scripting
- Pat class B 1016 C language
- Day_01 传智健康项目-项目概述和环境搭建
- Introduction to JVM principle
猜你喜欢

SSM project construction
![[cocos2d-x] screenshot sharing function](/img/fc/e3d7e5ba164638e2c48bc4a52a7f13.png)
[cocos2d-x] screenshot sharing function

gplearn出现 assignment destination is read-only

图解 Google V8 # 18 :异步编程(一):V8是如何实现微任务的?

【Cocos2d-x】截图分享功能

Ant Usage Summary (II): description of related commands
![[cocos2d-x] custom ring menu](/img/fd/c18c39ae738f6c1d2b76b6c54dc654.png)
[cocos2d-x] custom ring menu

Machine learning 3-ridge regression, Lasso, variable selection technique

Redis 哨兵

Cloud native database is the future
随机推荐
[open source project] excel export Lua configuration table tool
Sorting out common problems after crawler deployment
Introduction to JVM principle
Microsoft interview question: creases in origami printing
【DaVinci Developer专题】-42-如何生成APP SWC的Template和Header文件
Cloud native database is the future
Day_05 传智健康项目-预约管理-预约设置
Long substring without repeating characters for leetcode topic resolution
论文笔记: 多标签学习 LSML
mongodb项目中可能出现的坑
Day_10 传智健康项目-权限控制、图形报表
又到半年总结时,IT人只想躺平
Machine learning 3-ridge regression, Lasso, variable selection technique
[DaVinci developer topic] -41-app how SWC reads and writes NVM block data
Day_03 传智健康项目-预约管理-检查组管理
Efficient office of fintech (I): automatic generation of trust plan specification
【Vivado那些事儿】XilinxCEDStore介绍
Day_ 08 smart health project - mobile terminal development - physical examination appointment
Day_07 传智健康项目-Freemarker
Radar canvas