Created
December 17, 2019 05:26
-
-
Save incker/4477ffcd83bf92378724f634b5342c4a to your computer and use it in GitHub Desktop.
dedup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dedup<T>(List<T> list, {removeLast: true}) { | |
int shift = removeLast ? 1 : 0; | |
T compareItem; | |
for (int i = list.length - 1; i >= 0; i--) { | |
if (compareItem == (compareItem = list[i])) { | |
list.removeAt(i + shift); | |
} | |
} | |
} | |
void dedupBy<T, I>(List<T> list, I Function(T) compare, {removeLast: true}) { | |
int shift = removeLast ? 1 : 0; | |
I compareItem; | |
for (int i = list.length - 1; i >= 0; i--) { | |
if (compareItem == (compareItem = compare(list[i]))) { | |
list.removeAt(i + shift); | |
} | |
} | |
} | |
void main() { | |
List<List<int>> list = [[1], [1], [2, 1], [2, 2]]; | |
print('$list'); | |
dedupBy(list, (innerList) => innerList[0]); | |
print('$list'); | |
print('\n removeLast: false \n'); | |
List<List<int>> list2 = [[1], [1], [2, 1], [2, 2]]; | |
print('$list2'); | |
dedupBy(list2, (innerList) => innerList[0], removeLast: false); | |
print('$list2'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment