@@ -479,4 +479,69 @@ library Arrays {
479
479
sstore (array.slot, len)
480
480
}
481
481
}
482
+
483
+ /**
484
+ * @dev Removes duplicate values from a sorted array. This function does not check that the array is sorted,
485
+ * behavior is undefined if the array is not sorted. The resulting array will have no duplicates and will
486
+ * be shorter or the same length as the input array.
487
+ *
488
+ * This operation is performed in-place by moving elements and modifying the length of the array.
489
+ * Time complexity O(n).
490
+ *
491
+ * WARNING: This function is destructive. It will modify the array passed by reference.
492
+ */
493
+ function uniquifySorted (uint256 [] memory array ) internal pure returns (uint256 [] memory ) {
494
+ if (array.length <= 1 ) {
495
+ return array;
496
+ }
497
+
498
+ uint256 resultSize = 1 ;
499
+ for (uint256 i = 1 ; i < array.length ; ++ i) {
500
+ if (array[i] != array[i - 1 ]) {
501
+ if (i != resultSize) {
502
+ array[resultSize] = array[i];
503
+ }
504
+ ++ resultSize;
505
+ }
506
+ }
507
+
508
+ // Resize the array by creating a new one (can't modify length of memory arrays directly)
509
+ uint256 [] memory result = new uint256 [](resultSize);
510
+ for (uint256 i = 0 ; i < resultSize; ++ i) {
511
+ result[i] = array[i];
512
+ }
513
+ return result;
514
+ }
515
+
516
+ /**
517
+ * @dev Removes duplicate values from a sorted address array. This function does not check that the array
518
+ * is sorted, behavior is undefined if the array is not sorted. The resulting array will have no duplicates
519
+ * and will be shorter or the same length as the input array.
520
+ *
521
+ * This operation is performed in-place by moving elements and modifying the length of the array.
522
+ * Time complexity O(n).
523
+ *
524
+ * WARNING: This function is destructive. It will modify the array passed by reference.
525
+ */
526
+ function uniquifySorted (address [] memory array ) internal pure returns (address [] memory ) {
527
+ uint256 [] memory castedArray = _castToUint256Array (array);
528
+ uniquifySorted (castedArray);
529
+ return array;
530
+ }
531
+
532
+ /**
533
+ * @dev Removes duplicate values from a sorted bytes32 array. This function does not check that the array
534
+ * is sorted, behavior is undefined if the array is not sorted. The resulting array will have no duplicates
535
+ * and will be shorter or the same length as the input array.
536
+ *
537
+ * This operation is performed in-place by moving elements and modifying the length of the array.
538
+ * Time complexity O(n).
539
+ *
540
+ * WARNING: This function is destructive. It will modify the array passed by reference.
541
+ */
542
+ function uniquifySorted (bytes32 [] memory array ) internal pure returns (bytes32 [] memory ) {
543
+ uint256 [] memory castedArray = _castToUint256Array (array);
544
+ uniquifySorted (castedArray);
545
+ return array;
546
+ }
482
547
}
0 commit comments