【问题标题】:find two or more same elements in an array?在数组中找到两个或多个相同的元素?
【发布时间】:2017-09-19 15:05:14
【问题描述】:

假设我有一个像这样的数组 a[6] = {1,2,1,3,2,1,5} 我想打印 1,2。 我试过了:

#include<iostream>
using namespace std;
int main()
{
    int a[50] = {1,2,3,43,5,1,6,7,4,43,1};
    int c=0,b = sizeof(a)/sizeof(int);
    for(int y=0;y<11;y++)
    {

        for(int q = y+1;q<11;q++)
        {
                        if(a[y]=a[q])
                        {
                                  cout<<a[q]<<endl;

                        }
                    }

        }
    }

输出:1,1,1,1,2。 如果我设置像 {1,2,3,4,1,2} 这样的元素 它给出输出:1,2。

请帮助我。我只想将两个或多个重复的数字打印一次。

【问题讨论】:

  • Build a histogram。然后打印每个值为 2 或更大的元素。
  • 您可以添加到某种set,然后再打印集合中的所有项目,而不是立即打印。
  • 另外,仅供参考,a[y]=a[q] 行不符合我的预期 (for reference)
  • 使用std::map 或对您的数组(或它的副本)进行排序,那么您的任务将是微不足道的。

标签: c++ arrays for-loop


【解决方案1】:

注意这个if语句中的错别字

if(a[y]=a[q])
      ^^^

必须有比较运算符而不是赋值。

尽管如此,如果你更新你的程序,它的算法仍然是错误的。

使用循环的简单方法可以如下所示(我假设数组本身可能不会更改)

#include <iostream>

int main() 
{
    int a[] = { 1, 2, 3, 43, 5, 1, 6, 7, 4, 43, 1 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for ( size_t i = 0; i < N - 1; i++ )
    {
        size_t j = 0;
        while ( j < i && a[j] != a[i] ) j++;

        if ( j++ == i )
        {
            while ( j != N && a[j] != a[i] ) j++;
            if ( j != N ) std::cout << a[j] << ' ';
        }
    }

    std::cout << std::endl;

    return 0;
}

程序输出是

1 43 

即对于数组的每个当前元素,你需要检查它是否提前遇到。

另一种方法是使用标准容器std::map

例如

#include <iostream>
#include <map>
#include <iterator>

int main() 
{
    int a[] = { 1, 2, 3, 43, 5, 1, 6, 7, 4, 43, 1 };
    std::map<int, size_t> m;

    for ( int x : a ) ++m[x];

    for ( const auto &p : m )
    {
        if ( p.second != 1 ) std::cout << p.first << ' ';
    }

    std::cout << std::endl;

    return 0;
}

【讨论】:

    【解决方案2】:

    一个稍微有点 c++ish 的解决方案是

    #include <iostream>
    #include <vector>
    #include <map>
    
    int main()
    {
        std::vector<int> a =  { 1, 2, 3, 43, 5, 1, 6, 7, 4, 43, 1 };
        std::map<int,int> count;
    
        for(const auto& it:a)
        {
            auto hit = count[it]++;
            if(hit == 1)
                std::cout << it << " ";
        }
    }
    

    【讨论】:

      【解决方案3】:

      希望我的代码对你有帮助,

      我在代码上添加了标记,以帮助您了解我所做的事情。

      我的解决方案背后的想法是将数字保存在新数组中,然后打印元素。 如果您的数组具有特定功能(例如所有数字 >0 ),您可以将其用于标记元素(在 >0 的示例中,您可以使用 - 1 进行标记),然后传递数组。

      #include&lt;iostream&gt;

       //change the number 11 to define, for redability
       #define NUMBER_OF_ELEMENTS 11
          using namespace std;
          int main()
          {
      
      
          //new Parameters : dupArr[50] - save the duplicate numbers
          //                 count - tell us which is the last element at
          //                         b[50] that we add
          //                 iAddOneNumber - flag that tell us we add one element at  
          //                                 that iteration
      
      
              int dupArr[50]= {};
              int count=0;
              bool iAddOneNumber=false;
              int a[50] = {1,2,3,43,5,1,6,7,4,43,1,...,};
              int c=0,b = sizeof(a)/sizeof(int);
              for(int curElem=0;y< NUMBER_OF_ELEMENTS; curElem ++)
          {
      
              for(int chkElem = curElem +1; chkElem < NUMBER_OF_ELEMENTS; chkElem ++){
             // you assign here and you need to add more "=" for compare
                if(a[curElem]==a[chkElem]){
                   // instead of print from the condition my advice is to
                   // save in another place the numbers - for example - b[50]
                  if(iAddOneNumber==false){
                   dupArr[count]=a[curElem];
                   count++;
                   iAddOneNumber=true;
                  }
              }
            iAddOneNumber=false;
            }
          for(int i=0 ; i<NUMBER_OF_ELEMENTS ; i++){
           if(i<count){
              std::cout << dupArr[i];   
            }
          }
        }
      

      }

      【讨论】:

        【解决方案4】:

        标准库包含解决此问题所需的所有位。诀窍是首先对数组进行排序,以确保所有相等的元素彼此相邻。然后我们可以在数组中搜索相等的范围。

        #include <algorithm>
        #include <iostream>
        
        int a[] = {1,2,3,43,5,1,6,7,4,43,1};
        
        int main()
        {
          std::sort(std::begin(a), std::end(a));
        
          auto current = std::begin(a);
          while (current != std::end(a))
          {
            // If the current element is followed by 1 or more equal elements, then
            // we've found a repeated value.
            auto range = std::equal_range(current + 1, std::end(a), *current);
            if (range.first != range.second)
            {
              std::cout << *begin << std::endl;
            }
            current = range.second;
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-13
          • 2021-06-27
          • 2022-11-25
          • 1970-01-01
          相关资源
          最近更新 更多