Skip to content

Instantly share code, notes, and snippets.

@geektutor
Created May 16, 2020 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geektutor/1f678f85582f3a464ab4a2de06346cd2 to your computer and use it in GitHub Desktop.
Save geektutor/1f678f85582f3a464ab4a2de06346cd2 to your computer and use it in GitHub Desktop.
void main() {
unique([9, 3, 6, 4, 3, 4, 5]);
unique([1, 2, 3, 3, 3, 3, 4, 5, 3, 3, 3, 3, 4, 5, 6, 6, 11, 22, 33, 44]);
}
/* An implemention of selection sort in dart. Used Python knowledge for this */
unique(List sortList) {
List end = [];
for (var i = 0; i < sortList.length; i++) {
/* Loop through the list */
for (var j = i + 1; j < sortList.length; j++) {
if (sortList[i] > sortList[j]) {
/* Check if the ith item is greater than the jth item, if yes, swap positions */
int temp = sortList[i];
sortList[i] = sortList[j];
sortList[j] = temp;
}
}
if (!end.contains(sortList[i])) {
end.add(sortList[i]);
}
}
print("Sample List: $sortList");
print("Unique List: $end");
}
/*
Unique_Numbers
Create a function unique that takes a list(L) which contains numbers as a parameter and returns a sorted new list with unique elements of the first list.
Sample Inputs
1) unique([1,2,3,3,3,3,4,5])
2) unique([1,2,3,3,3,3,4,5,3,3,3,3,4,5,6,6,11,22,33,44])
3) unique([1,2,3,3,3,3,4,5,5,3,3,3,3,4,5,6,6,110,20,19,34])
Sample Output
1) Sample List: [1, 2, 3, 3, 3, 3, 4, 5]
Unique List: [1, 2, 3, 4, 5]
2) Sample List: [1, 2, 3, 3, 3, 3, 4, 5, 3, 3, 3, 3, 4, 5, 6, 6, 11, 22, 33, 44]
Unique List: [1, 2, 3, 4, 5, 6, 11, 22, 33, 44]
3) Sample List: [1, 2, 3, 3, 3, 3, 4, 5, 5, 3, 3, 3, 3, 4, 5, 6, 6, 110, 20, 19, 34]
Unique List: [1, 2, 3, 4, 5, 6, 19, 20, 34, 110]
NB: No built in module should be used.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment