Skip to content

Instantly share code, notes, and snippets.

@olumidayy
Last active May 16, 2020 04:12
Show Gist options
  • Save olumidayy/36824f013a204404960b280d00e4b95e to your computer and use it in GitHub Desktop.
Save olumidayy/36824f013a204404960b280d00e4b95e to your computer and use it in GitHub Desktop.
android day 15
void main() {
print(unique([1,5,9,2,2,11,333,567,2,4,5,5,3,5,6,4,8,9,10,32,12,56, 8]));
}
List<num> sorted(List<num> arr) {
/* This function takes in a List of numbers
* and returns the sorted list in ascending order
*
* NB: Based on the Comb Sort algorithm */
var l = arr.length, gap = l, shrink = 1.3;
bool sorted = false;
while (!sorted) {
gap ~/= shrink;
if (gap <= 1) {
sorted = true;
gap = 1;
}
for (var index = 0; index < l - gap; index++) {
var sm = gap + index;
if (arr[index] > arr[sm]) {
var swap = arr[index];
arr[index] = arr[sm];
arr[sm] = swap;
sorted = false;
}
}
}
return arr;
}
bool contains(List<num> arr, num n) {
/* takes in a list and a number and indicates with
* a boolean value whether the number is present in
* the list */
for (num i in arr) {
if (i == n) {
return true;
}
}
return false;
}
List<num> unique(List<num> arr) {
/* takes in a list of numbers, removes duplicates and
* returns the sorted list */
List<num> arr2 = [];
for (num n in arr) {
if (!(contains(arr2, n))) {
arr2 += [n];
}
}
return sorted(arr2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment