【问题标题】:Program to print duplicate values of array only once in C++在 C++ 中只打印一次数组的重复值的程序
【发布时间】:2021-03-28 19:44:18
【问题描述】:

int 数组[5]={3,3,3,3,1,1};

输出: 3 1

我无法做到这一点,请帮助我。我用 boolean check 尝试了 2 个 for 循环,但没有成功。

  int main()
  {
    int a[5]={3,3,3,1,1};
    int n=sizeof(a)/sizeof(a[0]);

    for (int i =0 ; i<n; i++){
       bool checked=true;
          for (int j=0 ;j<i; j++){
                 if(a[i]==a[j]){
                  bool checked=false;
        }
    }
    if(checked==true){
        cout<<a[i]<<",";
    }
}

}

【问题讨论】:

  • 请贴一些你试过的代码。
  • 如果您发布您尝试过的代码,那么有人会帮助您修复它,或者解释它有什么问题。这样你会学到更多。如果有人只是为您编写代码,那么没有什么可学的。
  • 两个带有布尔检查的 for 循环是解决此问题的一种方法,因此您可能非常接近。让我们看看你的代码!
  • 简单的错误。 bool checked=false; 不正确,您正在重新声明 checked 变量。改为checked=false;
  • @karan "It didn't work" 不是一个有用的问题描述。

标签: c++ arrays duplicates


【解决方案1】:

我使用一个函数从数组中删除重复值,然后打印没有重复的数组。我没有布尔函数。

int removeDuplicate (int arr[], int size) {
    if (size == 0 || size == 1) {
        return size; 
    }
    int* temp = new int[size]; /* Create a temporary
    array with size*/
    int j = 0;
    for (int i = 0; i < size - 1; i++) {
        if (arr[i] != arr[i + 1]) {
            temp[j++] = arr[i]; //strore element if its unique
        }
    }
    temp[j++] = arr[size - 1];
    for (int i = 0; i < j; i++) {
        arr[i] = temp[i]; //modify the existing array
    }
    return j; //j is the new size for the array without duplicates
}

int main(){
    int arr[5] = {3,3,3,1,1};
    
    int size = removeDuplicate(arr, 5);

    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ; ";
    }
}

【讨论】:

    【解决方案2】:

    请注意bool checked=false;您正在重新定义检查变量。

    请查看修复。

    
      int main()
      {
        int a[5]={3,3,3,1,1};
        int n=sizeof(a)/sizeof(a[0]);
    
        for (int i =0 ; i<n; i++)
        {
           bool checked=true;
            for (int j=i+1 ;j<n; j++)
            {
                if(a[i] == a[j])
                {
                    checked=false;
                }
            }
            if(checked == true)
            {
                std::cout<<a[i]<<",";
            }
        }
    }
    

    输出:3,1,

    即使您可以使用std::unique 算法以更好的方式做到这一点。

    #include <iostream>     // std::cout
    #include <algorithm>    // std::unique, std::distance
    #include <vector>       // std::vector
    
    int main () {
      int a[5]={3,3,3,1,1};         // 3 3 3 1 1
      std::vector<int> myvector (a, a+5);
    
      // Sorting the array 
      std::sort(myvector.begin(), myvector.end()); 
    
      // using default comparison:
      auto it = std::unique (myvector.begin(), myvector.end());   
                                                            
    
      myvector.resize( std::distance(myvector.begin(), it)); 
    
      // print out content:
      std::cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        std::cout << ' ' << *it;
      std::cout << '\n';
    
      return 0;
    }
    

    输出:myvector 包含:3 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-10
      • 2017-12-27
      • 2018-06-05
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2019-06-30
      相关资源
      最近更新 更多