find frequency in array using vector - c++

How can I change my code to get a count for every element?
With my code everything is okay. And it works, but how can I change only that part?
#include <iostream>
#include <vector>
void countFreq(int arr[], int n)
{
// Mark all array elements as not visited
std::vector<bool> visited(n, false);
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Skip this element if already processed
if (visited[i] == true)
continue;
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
visited[j] = true;
count++;
}
}
std::cout<<count<<" ";
}
}
int main()
{
int n;
std::cin>>n;
int arr[n];
for(int i = 0; i < n; i++){
std::cin>>arr[i];
}
countFreq(arr, n);
return 0;
}
And about the result`
input 10
1 1 2 2 3 3 4 4 5 5
output 2 2 2 2 2
but I want to get
output 2 2 2 2 2 2 2 2 2 2
(for every element)

Your function contains extra code that ends up confusing you. The visited variable is essentially unnecessary. Start the count at 0 and make no special case for the "current" cell and you'll find that some very simple code will do what you need:
void countFreq(int arr[], int n)
{
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Count frequency
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
std::cout << count << " ";
}
}

You need to save the result to an array for each number. Then when you find any processed number then print counter from the saved array.
#include <iostream>
#include <vector>
#include <unordered_map>
void countFreq(int arr[], int n)
{
// Mark all array elements as not visited
std::vector<bool> visited(n, false);
std::unordered_map<int, int> counter;
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++)
{
// Skip this element if already processed
if (visited[i] == true)
{
std::cout << counter[arr[i]] << " ";
continue;
}
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
visited[j] = true;
count++;
}
}
counter[arr[i]] = count;
std::cout<<count<<" ";
}
}
int main()
{
int n;
std::cin>>n;
int arr[n];
for(int i = 0; i < n; i++)
{
std::cin>>arr[i];
}
countFreq(arr, n);
return 0;
}

The issue is that you discard the values already visited.
One possibility is instead to memorize the count when the value is visited the first time,
and to memorize the index value of the first value appearance, when a value is visited the 2nd, 3rd ... time.
#include <iostream>
#include <vector>
void countFreq(const std::vector<int>& arr) {
int n = arr.size();
// Mark all array elements as not visited
std::vector<int> mem_count(n, n);
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Skip this element if already processed
if (mem_count[i] != n) {
std::cout << mem_count[mem_count[i]] << " ";
continue;
}
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
mem_count[j] = i;
count++;
}
}
mem_count[i] = count;
std::cout << count << " ";
}
}
int main() {
int n;
std::cin>>n;
std::vector<int> arr(n);
for(int i = 0; i < n; i++){
std::cin >> arr[i];
}
countFreq(arr);
return 0;
}

You can find the frequencies of numbers this way if you know the what is your maximum element in the input array. lets say m is maximum number in your array.
so you have to create a new array of size m. you can simply co-relate them as m buckets. from 0 to m. And each bucket will hold the count of each element in the input array. The index of each bucket will refer to element in the input array. This has time complexity O(1) if we know what is the max element the array.
You can do this way:
std::vector<int> frequencey(std::vector<int>& nums){
auto max = *(std::max_element(nums.begin(), nums.end()));
std::vector<int> frequencies(max + 1, 0);
for(int i = 0; i < nums.size(); ++i){
frequencies[nums[i]] +=1;
}
return frequencies;
}

This is very simple
#include <vector>
#include <map>
#include <iostream>
void main()
{
std::vector<int> v { 1,1,2,2,3,3,4,4,5,5 }; // Your input vector
// Count "frequencies"
std::map<int, int> m;
for (auto i : v)
m[i]++;
// Print output
for (auto i : v)
std::cout << m[i] << " ";
}

Related

Showing the original index of an element in a vector after bubblesort

I'm new to c++ and i'm having a problem with my code. I need to show the original indexes of a vector before it was sorted, after sorted. I tried it like this:
#include <vector>
using namespace std;
void bubblesort(vector<int> &a, int n) {
for (int j = 0; j < n - 1; j++) {
for (int i = n - 1; i > j; i--) {
if (a.at(i) < a.at(i-1)) {
int aux = a.at(i);
a.at(i) = a.at(i-1);
a.at(i-1) = aux;
}
}
}
}
int main()
{
int n;
cout << "Digite o tamanho do vetor: ";
cin >> n;
vector<int> v;
vector<int> vold;
vector<int> pos;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
v.push_back(a);
vold.push_back(a);
}
bubblesort(v, n);
for (int i = 0; i < n; i++) {
if (vold.at(i) == v.at(i)) {
pos.push_back(i);
}
else {
for (int j = i+1; j < n - 1; j++) {
if (vold.at(i) == v.at(j)) {
pos.at(j) = i;
break;
}
}
}
}
for (const int& i : pos) {
cout << i << " ";
}
system("pause>0");
}
But it didn't worked, if someone could help me to see what I'm doing wrong I would be glad, thanks in advance.
If your goal is to show the indices of the sorted vector, then another approach is to not sort the original vector, but instead to sort a vector of index values based on the original vector.
The index vector would be initialized to 0, 1, 2, etc. up until the vector's size, minus 1.
Here is an example:
#include <vector>
#include <numeric>
#include <iostream>
void bubblesort(std::vector<int> &a, std::vector<int>& index)
{
// Make sure the index vector is the same size as
// the original
index.resize(a.size());
if ( a.size() <= 1 )
return;
// This is just a shortcut way of setting the values to 0,1,2,etc.
std::iota(index.begin(), index.end(), 0);
size_t n = a.size();
// Here is your sort, but with one difference...
for (size_t j = 0; j < n - 1; j++)
{
for (size_t i = n - 1; i > j; i--)
{
// Look at the comparison being done here using the index array
if (a.at(index[i]) < a.at(index[i-1]))
{
// We swap the index values, not the values
// in the vector
int aux = index.at(i);
index.at(i) = index.at(i-1);
index.at(i-1) = aux;
}
}
}
}
int main()
{
std::vector<int> v = {3, 1, 65, 23, 4};
std::vector<int> index;
bubblesort(v, index);
// Display the index values of the sorted items
for (const int& i : index)
std::cout << i << " ";
}
Output:
1 0 4 3 2
Note that the bubblesort function takes a vector of indices, and not n. There is no need to pass n, since a vector already knows its own size by utilizing the size() function.
The output shows the original index of each of the sorted items.

print duplicate elements in array

I want to find duplicate elements in a dynamic array. In most cases it works, but how can it work for this case. I'm finding duplicate elements in a given array. But there is one problem that my duplicate elements can repeat too. How can I solve that part.
#include <iostream>
int main() {
unsigned n;
std::cin >> n;
int count = 0;
int* dynArr = new int[n];
for (int i = 0; i < n; i++) {
std::cin >> dynArr[i];
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(dynArr[j] == dynArr[i]){
std::cout<<dynArr[j]<<" ";
break;
}
}
}
}
I have a problem with this part. When I'm inputting a my array length 6, and elements {1,1,2,1,2,2}.
I got (1,1,2,2).
But I need to get only 1,2.
input 6
1 1 2 1 2 2
output 1 1 2 2
but must be
output 1 2
Here's a simple and fast solution.
std::map<int,size_t> element_count;
for(int i = 0; i < n; i++){
if ( ++element_count[dynArr[i]] == 2 ){
// Only report on the second occurrance
std::cout<<dynArr[j]<<" ";
}
}
const int listSize = 5;
int list1[listSize] = {0,0,1,1,3};
bool dupList[listSize] = {false};
for (int index = 0; index < listSize; index++) {
int val = list1[index];
for (int i = 0; i < listSize; i++) {
if (i != index) {
if (list1[i] == val) {
dupList[index] = true;
}
}
}
}
int printList[listSize];
int addNum = 0;
for (int index = 0; index < listSize; index++) {
if (dupList[index] == true) {
bool run = true;
int print = list1[index];
for (int i = 0; i < listSize; i++) {
if (printList[i] == print) {
run = false;
}
}
if (run == true) {
std::cout << print << " ";
printList[addNum] = print;
addNum++;
}
}
}
Note this code will NOT run quickly at all only use it if the operation does not need to get run very many times but it will only display the single numbers that are duplicates. It will defernatly need to get optimised more
You can simply get it done using two std::set<int>s.
#include <set>
#include <iostream>
int main() {
unsigned n;
std::cin >> n;
int count = 0;
std::set<int> all;
std::set<int> redundant;
for (int i = 0; i < n; i++) {
int input = 0;
std::cin >> input;
// You cant enter the entry because its present. This will return a std::pair<,> with the second value false.
if (!all.insert(input).second)
redundant.insert(input); // We store this in another set so we dont duplicate the redundant entries.
}
for (auto itr = redundant.begin(); itr != redundant.end(); itr++)
std::cout << *itr << " ";
}

Program to print duplicate values of array only once in C++

int array[5]={3,3,3,3,1,1};
output:
3 1
I am unable to do this, please help me out. I tried 2 for loops with boolean check , it didn't work.
int main()
{
int a[5]={3,3,3,1,1};
int n=sizeof(a)/sizeof(a[0]);
for (int i =0 ; i<n; i++){
bool checked=true;
for (int j=0 ;j<i; j++){
if(a[i]==a[j]){
bool checked=false;
}
}
if(checked==true){
cout<<a[i]<<",";
}
}
}
I used a function to remove duplicate values from the array and then printed the array without the duplicates. I don't have boolean functions.
int removeDuplicate (int arr[], int size) {
if (size == 0 || size == 1) {
return size;
}
int* temp = new int[size]; /* Create a temporary
array with size*/
int j = 0;
for (int i = 0; i < size - 1; i++) {
if (arr[i] != arr[i + 1]) {
temp[j++] = arr[i]; //strore element if its unique
}
}
temp[j++] = arr[size - 1];
for (int i = 0; i < j; i++) {
arr[i] = temp[i]; //modify the existing array
}
return j; //j is the new size for the array without duplicates
}
int main(){
int arr[5] = {3,3,3,1,1};
int size = removeDuplicate(arr, 5);
for (int i = 0; i < size; i++) {
cout << arr[i] << " ; ";
}
}
Please note bool checked=false; you are defining again checked variable.
Please see the fix.
int main()
{
int a[5]={3,3,3,1,1};
int n=sizeof(a)/sizeof(a[0]);
for (int i =0 ; i<n; i++)
{
bool checked=true;
for (int j=i+1 ;j<n; j++)
{
if(a[i] == a[j])
{
checked=false;
}
}
if(checked == true)
{
std::cout<<a[i]<<",";
}
}
}
output : 3,1,
Even you can do it in a better way with std::unique algorithm.
#include <iostream> // std::cout
#include <algorithm> // std::unique, std::distance
#include <vector> // std::vector
int main () {
int a[5]={3,3,3,1,1}; // 3 3 3 1 1
std::vector<int> myvector (a, a+5);
// Sorting the array
std::sort(myvector.begin(), myvector.end());
// using default comparison:
auto it = std::unique (myvector.begin(), myvector.end());
myvector.resize( std::distance(myvector.begin(), it));
// print out content:
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
output : myvector contains: 3 1

C++ Selection Sort (vectors)

int temp;
for (int j = 0; j < vecsize - 1; ++j) {
int min = sort.at(j);
for (int i = j+1; i < vecsize; ++i) {
if (min > sort.at(i)) {
min = sort.at(i);
temp = i;
}
}
swap(sort.at(j), sort.at(temp));
}
I am trying to sort (in ascending order) the vector of: 23 42 4 16 8 15
However, my attempt at using selection sort outputs: 4 8 15 23 16 42
What am I doing wrong?
When you define min, you seem to be assigning it the value of the array sort at jth index. Yet, you are using an extra variable tmp to swap the elements, and you seem to fail to initialize it before the inner for loop, similar to how you initialize min. And if all the other elements in the array are smaller than the element at sort[j], tmp will be uninitialized for that iteration of the outer loop, possibly causing it to have an incorrect value in it.
int temp;
for (int j = 0; j < vecsize - 1; ++j) {
int min = sort.at(j);
temp = j; # HERE'S WHAT'S NEW
for (int i = j+1; i < vecsize; ++i) {
if (min > sort.at(i)) {
min = sort.at(i);
temp = i;
}
}
swap(sort.at(j), sort.at(temp));
}
You may see this code at work here. It seems to produce the desired output.
Try this : corrected-code
#include <iostream>
#include <vector>
using namespace std;
void print (vector<int> & vec) {
for (int i =0 ; i < vec.size(); ++i) {
cout << vec[i] << " ";
}
cout << endl;
}
int main() {
int temp;
vector<int> sort;
sort.push_back(23);
sort.push_back(42);
sort.push_back( 4);
sort.push_back( 16);
sort.push_back( 8);
sort.push_back(15);
print(sort);
int vecsize = sort.size();
for (int j = 0; j < vecsize - 1; ++j) {
int min = j;
for (int i = j+1; i < vecsize; ++i) {
if (sort.at(min) > sort.at(i)) {
min = i;
}
}
if (min != j)
swap(sort.at(j), sort.at(min));
}
print(sort);
return 0;
}
If you can use C++11, you can also solve sorting (as in your example) with lambdas. It's a more powerful and optimized way. You should try it maybe in the future.
[EDITED]:
A short example:
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> myVector;
myVector.emplace_back(23);
myVector.emplace_back(42);
myVector.emplace_back(4);
myVector.emplace_back(16);
myVector.emplace_back(8);
myVector.emplace_back(15);
std::sort(myVector.begin(), myVector.end(),
[](int a, int b) -> bool
{
return a < b;
});
}

Frequency of Numbers in a 1D Array

its been 6 hours since I have been writing the code but to no avail, I don't no where I am making the mistake but I am making some. Its a frequency output program and output should be like this:
array[8] = {6,1,7,8,6,6,1,9}
Output:
6:3
1:2
7:1
8:1
9:1
But its repeating the same numbers in my code. Any help would be much appreciable.
int array[8] = {6,1,7,8,6,6,1,9};
int store[8];
int a =0;
int b =0;
int c=0;
int d = 0;
store[d] = array[b];
for (d = 0; d < 8; d++){
int count=0;
c = d;
b = d;
for (int e = 0; e < d; e++){
if (array[b] == store[e]){
store[d] = array[b];
b++;
e = 0;
}
else
{
store[d] = array[b];
break;
}
}
for ( int z = 0; z < 7; z++){
if (store[d] == array[z])
{
count++;
}
}
cout << store[d] << ":" << count << endl;
}
You may use a map first to store num->frequency and then a multimap to store freqeuncy => num.
Here is the working solution.
#include <map>
#include <algorithm>
#include <iostream>
int main()
{
int array[8] = {6,1,7,8,6,6,1,9};
// A map to store num => freq
std::map <int, int> freq;
// A map to store freq(can be duplicate) => num
std::multimap <int, int> freqCounts;
// Store num => frequency
for (int i = 0 ; i < 8; i++)
{
freq[array[i]] += 1;
}
// Now Store freq => num
for(auto const & iter : freq)
{
freqCounts.insert (std::pair<int,int>(iter.second, iter.first));
}
// Print in reverse order i.e. highest frequency first
for (std::multimap<int,int>::reverse_iterator rit=freqCounts.rbegin(); rit!=freqCounts.rend(); ++rit)
{
std::cout << rit->second << " : " << rit->first << '\n';
}
return 0;
}
You never seem to update the counters. Try this:
int array[8] = {6,1,7,8,6,6,1,9};
unsigned int store[10] = {}; // large enough to hold the largest array value,
// initialized to zero
for (int n : array) ++store[n]; // update counts
for (int i = 0; i != 10; ++i)
{
std::cout << "Frequency of int " << i << " is " << store[i] << "\n";
}
If the set of values that occur is sparse, or includes negatives, or simply does not fit into a dense range of integers nicely, you can replace unsigned int[10] with an associative container, e.g.:
std::map<int, unsigned int> store;
// algorithm as before
for (auto const & p : store)
{
std::cout << "Frequency of " << p.first << " is " << p.second << "\n";
}
I'm not sure what you are trying to do with the arrays. I have tried to follow the logic, but it's hard to see it with all the anonymous variable names. It seems like you are trying to look for duplicates earlier in the array, but the variable e never gets any other value than 0, so you will only be comparing with the first item in the array.
You can just look in the array itself for previous occurances, and once you know that the number is the first occurance, you only need to look for more occurances after it in the array:
int array[8] = {6,1,7,8,6,6,1,9};
for (int i = 0; i < 8; i++) {
// look to the left in the array if the number was used before
int found = 0;
for (int j = 0; j < i; j++) {
if (array[i] == array[j]) found++;
}
// go on if it's the first occurance
if (found == 0) {
// we know of one occurance
int count = 1;
// look to the right in the array for other occurances
for (int j = i + 1; j < 8; j++) {
if (array[i] == array[j]) count++;
}
cout << array[i] << ":" << count << endl;
}
}
I wanted to submit my solution which I think it´s a more easy one:
#include <iostream>
using namespace std;
int main() {
int n; //number of Elements in the vector
cin>>n;
int vec[n]; //vector with all elements
int v[n]; //vector with Elements without repetition
int c[n]; // vector which stores the frequency of each element
for(int i=0; i<n; i++)
cin>>vec[i];
int k=0; // number of Elements of the vector without Repetition, in the begining, it has 0 Elements
int j=0; //logic Counter, could be replaced with bool
for(int i=0; i<n; i++) {
for(int h=0; h<=k; h++) {
if(vec[i]==v[h]) {
c[h]++;
j=1;
break;
}
}
//if element i of the original vector is equal to element h of the second vector, then increment the frequency of this element
if(j==0) { //else if the element is not equal to any of the second vector, the Position of the 2nd vector is filled with the element, which in this case is the first of ist Kind.
v[k]=vec[i];
c[k]=1;
k++;
} //the number of Elements is increased by one to store another element;
else {
j=0;
}
}
cout<<endl<<endl;
for(int i=0; i<k; i++)
cout<<v[i]<<":"<<c[i]<<endl;
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
main()
{ int count[10],key[10],n=10,m;
int i,j,k,temp;
cout<<"Enter The Size Of Array:-\n";
cin>>n;
int a[n];
cout<<"Enter The Elements in Array:-\n";
for(i=0; i<n; i++)
cin>>a[i];
for(i=0; i<n; i++)
for(j=0; j<n-1; j++)
{ if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
for(i=0; i<n; i++)
cout<<a[i]<<"\t";
for(i=0,k=0; i<n; k++)
{ count[k]=0;
key[k]=a[i];
for(j=i; j<n; j++)
{ if(a[i]==a[j])
count[k]++;
}
i=i+count[k];
}
for(i=0; i<k; i++)
cout<<endl<<key[i]<<" Occurred "<<count[i]<<" Times\n";
getch();
}
/**
* The methods counts the frequency of each element in an array.
*
* Approach: The method checks if the element is already present in the <strong>Map of frequency</strong>.
* If it is not present, add it to the map with the frequency 1 else put it in the map with
* an increment by one of it's existing frequency.
*
* #param arr list of elements
* #return frequency of each elements
*/
public static Map<Integer, Integer> countFrequency(int[] arr) {
Map<Integer, Integer> frequency= new HashMap<Integer, Integer>();
for(int i = 0; i < arr.length; i++) {
if(frequency.get(arr[i])==null) {
frequency.put(arr[i], 1);
}
else {
frequency.put(arr[i],frequency.get(arr[i])+1);
}
}
System.out.println("\nMap: "+frequency);
return frequency;
}