Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to group elements of an array based on their first occurrence in C++

2 Answers

0 votes
#include <iostream>
#include <unordered_map>
 
void group_elements(int arr[], int len) {
    std::unordered_map<int, int> frequency;
  
    for (int i = 0; i < len; i++) {
        frequency[arr[i]]++;
        std::cout << "frequency[arr[" << i << "]] = " << frequency[arr[i]] << " arr[" << i << "] = " << arr[i] << "\n";
    }
  
    for (int i = 0; i < len; i++) {
        if (frequency.find(arr[i]) != frequency.end()) {
            int total_frequency = frequency[arr[i]];
            while (total_frequency--) {
                std::cout << arr[i] << " ";
            }
            frequency.erase(arr[i]);
        }
    }
}
  
int main()
{
    int arr[] = { 8, 3, 7, 8, 2, 5, 8, 5, 1, 9, 8, 1, 7 };
 
    group_elements(arr, sizeof(arr) / sizeof(arr[0]));
}
 
 
 
 
/*
run:
 
frequency[arr[0]] = 1 arr[0] = 8
frequency[arr[1]] = 1 arr[1] = 3
frequency[arr[2]] = 1 arr[2] = 7
frequency[arr[3]] = 2 arr[3] = 8
frequency[arr[4]] = 1 arr[4] = 2
frequency[arr[5]] = 1 arr[5] = 5
frequency[arr[6]] = 3 arr[6] = 8
frequency[arr[7]] = 2 arr[7] = 5
frequency[arr[8]] = 1 arr[8] = 1
frequency[arr[9]] = 1 arr[9] = 9
frequency[arr[10]] = 4 arr[10] = 8
frequency[arr[11]] = 2 arr[11] = 1
frequency[arr[12]] = 2 arr[12] = 7
8 8 8 8 3 7 7 2 5 5 1 1 9 
 
*/

 



answered Aug 14, 2022 by avibootz
edited Aug 14, 2022 by avibootz
0 votes
#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 
  
*/

 



answered Oct 10, 2025 by avibootz
...