#include#include int main() { int arr[] = {1, 2, 3, 2, 1, 1, 4}; std::unordered_map freq; for (int i = 0; i < 7; i++) { freq[arr[i]]++; } for (auto& it : freq) { std::cout << "Element " << it.first << " occurs " << it.second << " times\n"; } return 0; }
Element 1 occurs 3 times Element 2 occurs 2 times Element 3 occurs 1 times Element 4 occurs 1 times
#includeThis code implements the Fibonacci sequence recursively, but with memoization to avoid redundant calculations. The memoization is done using the `std::unordered_map`. This example also uses the C++ Standard Library.int fibonacci(int n, std::unordered_map & memo) { if (memo.count(n) != 0) { return memo[n]; } if (n == 0) { return 0; } if (n == 1) { return 1; } int result = fibonacci(n-1, memo) + fibonacci(n-2, memo); memo[n] = result; return result; } int main() { std::unordered_map memo; std::cout << fibonacci(10, memo) << std::endl; return 0; }