
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Array elements that appear more than once?
Here we will see one problem. We have one array. our task is to find those elements whose frequencies are more than 1. Suppose the elements are {1, 5, 2, 5, 3, 1, 5, 2, 7}. Here 1 has occurred 2 times, 5 has occurred 3 times and 2 has occurred three times, others have occurred only once. So the output will be {1, 5, 2}
Algorithm
moreFreq(arr, n)
Begin define map with int type key and int type value for each element e in arr, do increase map.key(arr).value done for each key check whether the value is more than 1, then print the key End
Example
#include <iostream> #include <map> using namespace std; void moreFreq(int arr[], int n){ map<int, int> freq_map; for(int i = 0; i<n; i++){ freq_map[arr[i]]++; //increase the frequency } for (auto it = freq_map.begin(); it != freq_map.end(); it++) { if (it->second > 1) cout << it->first << " "; } } int main() { int arr[] = {1, 5, 2, 5, 3, 1, 5, 2, 7}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Frequency more than one: "; moreFreq(arr, n); }
Output
Frequency more than one: 1 2 5
- Related Articles
- Array elements that appear more than once in C?
- JavaScript array: Find all elements that appear more than n times
- Elements that appear twice in array in JavaScript
- Frequency of elements of one array that appear in another array using JavaScript
- MySQL Select where value exists more than once
- Get count of values that only appear once in a MySQL column?
- MySQL query to find a value appearing more than once?
- Insert more than one element at once in a C# List
- addEventListener() not working more than once with a button in JavaScript?
- Program to count k length substring that occurs more than once in the given string in Python
- Compute the multiplicative inverse of more than one matrix at once in Python
- Can I play the same sound more than once at the same time with HTML5?
- Check if a cell can be visited more than once in a String in C++
- Find the element that appears once in sorted array - JavaScript
- C++ program to remove minimum elements from either side such that 2*min becomes more than max

Advertisements