Norway


Given an , find the least in it. If there are multiple elements that appear least number of times, print any one of them.

:

3;
Input : arr[] = {, 3, 2, , 2, 2, 3, }3;
Output : 33;
3 appears minimum number of times in given3;
array.3;
3;
Input : arr[] = {, 20, 30}
Output :  or 20 or 30

A simple solution is to run two loops. The outer loop picks all elements one by one. The inner loop finds frequency of the picked element and compares with the minimum so far. complexity of this solution is O(n2)

A better solution is to do sorting. We first sort the array, then linearly traverse the array.

// CPP  to find the least frequent element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int leastFrequent(int arr[], int n)
{
    // Sort the array
    sort(arr, arr + n);

    // find the min frequency using linear traversal
    int min_count = n+1, res = -1, curr_count = 1;
    for (int i = 1; i < n; i++) {
        if (arr[i] == arr[i - 1])
            curr_count++;
        else {
            if (curr_count < min_count) {
                min_count = curr_count;
                res = arr[i - 1];
            }
            curr_count = 1;
        }
    }
 
    // If last element is least frequent
    if (curr_count < min_count)
    {
        min_count = curr_count;
        res = arr[n - 1];
    }

    return res;
}

// driver 
int main()
{
    int arr[] = {1, 3, 2, 1, 2, 2, 3, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << leastFrequent(arr, n);
    return 0;
}

Time Complexity : O(n Log n)
Auxiliary Space : O(1)

An efficient solution is to use hashing. We create a hash table and store elements and their frequency counts as key value pairs. Finally we traverse the hash table and print the key with minimum value.

// CPP program to find the least frequent element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int leastFrequent(int arr[], int n)
{
    // Insert all elements in hash.
    unordered_map<int, int> hash;
    for (int i = 0; i < n; i++)
        hash[arr[i]]++;

    // find the min frequency
    int min_count = n+1, res = -1;
    for (auto i : hash) {
        if (min_count >= i.second) {
            res = i.first;
            min_count = i.second;
        }
    }

    return res;
}

// driver program
int main()
{
    int arr[] = {1, 3, 2, 1, 2, 2, 3, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << leastFrequent(arr, n);
    return 0;
}

Time Complexity : O(n)
Auxiliary Space : O(n)


- rx58pta1hoijjjxu7p5f - Least frequent element in an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here