Question

I have to find least common number from an int array , I have written code but it is not working properly ,

Here is my logic, 1. sort the array 2. get min common counter updated 3. get if all are unique

and the code below,

static int min_loc ; //minimum value location 
static int min_cnt ;
int all_uniqFlag = true;

void leastCommon(int data[],int n)
{
   int rcount = 0; //Repeated number counter
   int mcount = n; // minimum repetetion counter;

   // The array is already sorted we need to only find the least common value. 
   for(int i = 0 ; i < n-1 ; i++)
   {  
      //Case A : 1 1 2 2 2 3 3 3 3 4 5 5 5 5 : result should be 4 
      //Case B : 1 2 3 4 5 6 7 (All unique number and common values so all values should be printed
      //                        and ) 
      //Case C : 1 1 2 2 3 3 4 4 (all numbers have same frequency so need to display all )
      cout << "data[i] : " << data[i] << " data[i+1] : " << data[i+1] <<  "i = " << i << endl;
      if(data[i] != data[i+1])
      {
         //mcount = 0;
         //min_loc = i; 
         //return;
      }
      if(data[i] == data[i+1])
      {
        all_uniqFlag = false;
        rcount++;
      }
      else if(rcount < mcount)
      {
         mcount = rcount;
         min_loc = i ;//data[i];
      }
   } 
   min_cnt = mcount;   
}

As mentioned in the comment only Case B works and Case A and C is not working could you help me fix the issue ?

Was it helpful?

Solution

  • scan through the list
  • compare each element in the list with the last element in the out array
  • If the element matches, then increment its count by 1
  • If the element doesn't match then add the new element into out array and increment index by 1

Once the scan is done, the out array will have all the distinct elementsout[][0] and their frequencies out[][1]

  • Scan through the frequency list (out[][1]) to find the lowest frequency
  • Finally do another scan through the element list out[][0] and print elements whose frequency matches with the lowest frequency

.

#include<stdio.h>
#include<stdlib.h>
#define N 8
int main()
{
    //int data[N]={1,2,3,4,5,6,7};
    int data[N]={1,1,2,2,3,3,4,4};
    //int data[N]={1,1,2,2,2,3,3,3,3,4,5,5,5,5};
    int out[N][2];
    int i=0,index=0;
    for(i=0;i<N;i++)
    {
        out[i][0]=0; 
        out[i][1]=0; 
    }
    out[0][0] = data[0];
    out[0][1]=1;
    for(i=1;i<N;i++)
    {
        if(data[i] != out[index][0])
        {
            index++;
            out[index][0] = data[i];
            out[index][1] = 1;
        }
        else
        {
            out[index][1]++;
        }
    }

    int min=65536;
    for(i=0;i<N;i++)
    {
        if(out[i][1] == 0)
        {
            break;
        }
        if(out[i][1] < min)
        {
            min = out[i][1];
        }
    }
    for(i=0;i<N;i++)
    {
        if(out[i][1] == min)
        {
            printf("%d\t",out[i][0]);
        }
    }
    printf("\n");
}

OTHER TIPS

You can use a map for this:

#include <string>
#include <map>
#include <iostream>

typedef std::map<int, int> Counter;

void leastCommon(int data[],int n) {
    Counter counter;
    int min = n;
    for (int i = 0; i < n; i++)
        counter[data[i]]++;
    for (Counter::iterator it = counter.begin(); it != counter.end(); it++) {
        if (min > it->second) min = it->second;
    }   
    for (int i = 0; i < n; i++) {
        if (counter[data[i]] == min) {
            std::cout << data[i] << std::endl;
            counter[data[i]]++;
        }   
    }   
}

int main() {
    int data[] = {1, 1,3,4,4,2,4,3,2};
    leastCommon(data, 9); 
    return 0;
}

Approach is-

  • select 1st element from the sorted array, and while consecutive elements to it are same, store them in output[] until the loop breaks
  • store the frequency of element in leastFrequency
  • select next element, check with its consecutive ones and store them in same output[] until the loop breaks
  • check frequency of this with the leastFrequency
    • if same, do nothing (let these be added in the output[])
    • if less, clear output[] and store the element same no. of times
    • if more, change the effective output[] length to previous length before iterating for this element
  • similarly iterate for all distinct elements and finally get the result from output[] from 0 to effective length

    void leastCommon(int data[], int len) {
    
    if ( len > 0) {
        int output[] = new int[len];
        int outlen = 0; // stores the size of useful-output array
        int leastFrequency = len; // stores the lowest frequency of elements
    
        int i=0;
        int now = data[i];
        while (i < len) {
            int num = now;
            int count = 0;
            do {
                output[outlen] = now;
                outlen++;
                count++;
    
                if((++i == len)){
                    break;
                }
                now = data[i];
            } while (num == now);   // while now and next are same it adds them to output[] 
    
            if (i - count == 0) { // avoids copy of same values to output[] for 1st iteration
                leastFrequency = count;
            } else if (count < leastFrequency) {  // if count for the element is less than the current minimum then re-creates the output[]
                leastFrequency = count;
                output = new int[len];
                outlen = 0;
                for (; outlen < leastFrequency; outlen++) {    
                    output[outlen] = num;   // populates the output[] with lower frequent element, to its count 
                }
            } else if (count > leastFrequency) {
                outlen -= count;    // marks outlen to its same frequent numbers, i.e., discarding higher frequency values from output[]
            }
        }
        //for(int j = 0; j < outlen; j++) {
        // print output[] to console
        //}
      }
    }
    

Plz suggest for improvements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top