Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main() {
- int size;
- // Accept the size of the array
- std::cout << "Enter the size of the array: ";
- std::cin >> size;
- // Dynamically allocate the array
- int* arr = new int[size];
- // Accept elements of the array
- std::cout << "Enter " << size << " elements of the array: ";
- for (int i = 0; i < size; i++) {
- std::cin >> arr[i];
- }
- // Step 1: Detect all numbers with duplicates
- const int RANGE = 1000; // Adjust RANGE for the expected value range
- int count[RANGE] = {0};
- for (int i = 0; i < size; i++) {
- count[arr[i]]++; // Count occurrences of each number
- }
- // Mark numbers with duplicates
- bool hasDuplicates[RANGE] = {false};
- for (int i = 0; i < RANGE; i++) {
- if (count[i] > 1) {
- hasDuplicates[i] = true;
- }
- }
- // Step 2: Remove only one duplicate of each detected duplicate number from the end
- for (int i = size - 1; i >= 0; i--) {
- if (hasDuplicates[arr[i]]) {
- hasDuplicates[arr[i]] = false; // Ensure only one is removed
- // Shift elements to the left
- for (int j = i; j < size - 1; j++) {
- arr[j] = arr[j + 1];
- }
- size--; // Reduce logical size
- }
- }
- // Output the resulting array
- std::cout << "Resulting array: ";
- for (int i = 0; i < size; i++) {
- std::cout << arr[i] << " ";
- }
- std::cout << std::endl;
- // Free allocated memory
- delete[] arr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement