int数组中最不常见的公共数

时间:2014-04-24 02:20:30

标签: c++ arrays algorithm data-structures array-algorithms

我必须从int数组中找到最不常见的数字,我已编写代码但它无法正常工作,

这是我的逻辑, 1.排序数组 2.让min min计数器更新 3.如果所有都是唯一的

和下面的代码,

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;   
}

正如评论中所提到的,只有案例B有效且案例A和案例C不起作用你能帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

您可以使用map

#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;
}

答案 1 :(得分:1)

  • 浏览列表
  • 将列表中的每个元素与out数组
  • 中的最后一个元素进行比较
  • 如果元素匹配,则将其计数增加1
  • 如果元素不匹配,则将新元素添加到out 数组和增量index乘以1

扫描完成后,out数组将包含所有不同的元素out[][0]及其频率out[][1]

  • 扫描频率列表(out[][1])以查找最低频率
  • 最后对元素列表out[][0]进行另一次扫描,并打印频率与最低频率匹配的元素

#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");
}

答案 2 :(得分:1)

方法是 -

  • 从排序数组中选择第一个元素,并且当连续元素相同时,将它们存储在output []中,直到循环中断
  • 将元素的频率存储在leastFrequency
  • 选择下一个元素,检查其连续元素并将它们存储在相同的输出[]中,直到循环中断
  • 以最小频率检查此频率
    • 如果相同,什么都不做(让这些添加到输出[])
    • 如果更少,清除输出[]并存储元素相同的否。时间
    • 如果更多,请在迭代此元素之前将有效输出[]长度更改为先前的长度
  • 类似地迭代所有不同的元素,最后将output []的结果从0变为有效长度

    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建议改进。