Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <utility>
- using namespace std;
- int remove_duplicates(int*, int);
- int search_duplicat(int*, const int, int, int);
- void remove_element(int*, const int, int);
- void print(int*, const int);
- int main() {
- const int buffer = 11;
- int arr[buffer] = { 1, 2, 1, 3, 2, 1, 2, 1, 2, 1, 2 };
- int size = buffer;
- print(arr, size);
- size = remove_duplicates(arr, size);
- print(arr, size);
- cin.get();
- }
- int remove_duplicates(int* arr, int size) {
- int index;
- for (int i = 0; i < size - 1; ++i) {
- for (int j = i + 1; j < size; ++j) {
- index = 0;
- while ((index = search_duplicat(arr, size, arr[i], j)) != -1) {
- if (index) {
- remove_element(arr, size, index);
- --size;
- }
- }
- if (index = -1) break;
- }
- }
- return size;
- }
- int search_duplicat(int* arr, const int size, int element, int beg) {
- if (beg >= size) return -1;
- do if (arr[beg] == element) return beg; while (++beg < size);
- return -1;
- }
- void remove_element(int* arr, const int size, int index) {
- for (int i = index; i < size - 1; ++i) swap(arr[i], arr[i + 1]);
- }
- void print(int* arr, const int size) {
- for (int i = 0; i < size; ++i) cout << ' ' << arr[i];
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement