I want to remove duplicates from array using a single loop, it's not working, This is what I've done so far.
Please note that I already know it works on sorted array, I use a single loop bubble sort for that, but I want it to work without sorting.
code.c
#include <stdio.h>
#define size 20
#define true 1
#define false 0
int main() {
int input[size] = {1, 3, 3, 3, 3, 3, 4, 3, 3, 3, 5, 6, 7, 8, 1, 1, 1, 1, 2, 2};
int current = input[0], flag = false, index = 0;
for (int x = 0; x < size; x++) {
if (current == input[x] && (flag == false)) {
flag = true;
} else if (current != input[x]) {
input[index++] = current;
current = input[x];
flag = false;
}
}
for (int foo = 0; foo < index; foo++) {
printf("%d", input[foo]);
printf((foo != index - 1) ? ", " : "");
}
return 0;
}
input
1, 3, 3, 3, 3, 3, 4, 3, 3, 3, 5, 6, 7, 8, 1, 1, 1, 1, 2, 2
output
1, 3, 4, 3, 5, 6, 7, 8, 1
calloca status bit array, check if previously found and mark off. If the next value exceeds the range,reallocand clear the new elements.for), or even no loops (recursion). It's much more important to know which time you're aiming for: O(1), O(logn), O(n), O(n²)? etc... Because you may use recursion, one loop, gotos and what-not, but in the end, you will have a time boundary, which is what matters for sorting/removing duplicates/searching, etc...