C++ Online Compiler
Example: FrequencyCount_NestedLoops in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// FrequencyCount_NestedLoops #include <iostream> #include <vector> // Using std::vector for dynamic array and boolean tracking int main() { // Step 1: Initialize the array std::vector<int> arr = {5, 2, 8, 5, 1, 2, 5}; int n = arr.size(); // Step 2: Create a boolean vector to keep track of visited elements std::vector<bool> visited(n, false); std::cout << "Element frequencies using Nested Loops:" << std::endl; // Step 3: Iterate through the array for (int i = 0; i < n; i++) { // If the element has already been visited, skip it if (visited[i] == true) { continue; } // Step 4: Initialize count for the current element int count = 1; // Step 5: Iterate from the next element to count occurrences for (int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { count++; visited[j] = true; // Mark as visited to avoid re-counting } } std::cout << "Element " << arr[i] << ": " << count << " time(s)" << std::endl; } return 0; }
Output
Clear
ADVERTISEMENTS