Answer to Question #161234 in C++ for Ashes

Question #161234

Write a C++ program to find the least occurring element in an array of integers.


1
Expert's answer
2021-02-06T10:10:05-0500
#include <iostream>
using namespace std;
int main()
{
    int arr[] = {2, 3, 4, 4, 5, 5, 6, 2, 3};
    int arrsize = sizeof(arr)/sizeof(*arr);
    int min_freq = arrsize, least_element = arr[0], freq = 0;
    
    for(int i=0; i<arrsize; i++){
        freq = 0;
        for(int j=0; j<arrsize; j++){
            if(arr[i] == arr[j]){
                freq++;
            }
        }
        if(freq < min_freq){
            min_freq = freq;
            least_element = arr[i];
        }
    }
    
    cout<<"The least occuring element is "<<least_element;
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog