#include <iostream>
#define SIZE 13
#define MAX_VALUE 100 // Assumes values are between 0 and 99
// Function to group elements by their first occurrence
int groupElements(const int arr[], int len, int result[]) {
int frequency[MAX_VALUE] = {0};
int order[MAX_VALUE];
int orderCount = 0;
int resultCount = 0;
// Count frequencies and track order of first occurrences
for (int i = 0; i < len; i++) {
int num = arr[i];
if (frequency[num] == 0) {
order[orderCount++] = num;
}
frequency[num]++;
}
// Group elements based on their first occurrence
for (int i = 0; i < orderCount; i++) {
int num = order[i];
for (int j = 0; j < frequency[num]; j++) {
result[resultCount++] = num;
}
}
return resultCount; // Return the number of elements in result
}
int main() {
int arr[SIZE] = {88, 33, 77, 88, 22, 55, 88, 55, 11, 99, 88, 11, 77};
int grouped[SIZE]; // Result array
int groupedSize = groupElements(arr, SIZE, grouped);
std::cout << "Grouped vector: ";
for (int i = 0; i < groupedSize; i++) {
std::cout << grouped[i] << " ";
}
}
/*
run:
Grouped vector: 88 88 88 88 33 77 77 22 55 55 11 11 99
*/