Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
945 B
3
Indexable
Never
#include<bits/stdc++.h>
using namespace std;
class ArrayWithDuplicates {
public:
    ArrayWithDuplicates(int size) : size_(size) {
        arr_ = new int[size];
    }


    void inputArray() {
        cout << "Enter " << size_ << " elements:\n";
        for (int i = 0; i < size_; ++i) {
            cin >> arr_[i];
        }
    }

    void removeDuplicates() {
        int uniqueSize = unique(arr_, arr_ + size_) - arr_;
        size_ = uniqueSize;
    }

    void printArray() {
        cout << "Array after removing duplicates:\n";
        for (int i = 0; i < size_; ++i) {
            cout << arr_[i] << " ";
        }
        cout << endl;
    }

private:
    int size_;
    int* arr_;
};

int main() {
    int size;
    cout << "Enter array size: ";
    cin >> size;

    ArrayWithDuplicates arr(size);
    arr.inputArray();
    arr.removeDuplicates();
    arr.printArray();

    return 0;
}
Leave a Comment