Find unique numbers in array

后端 未结 8 1139
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 20:14

Well, I have to find how many different numbers are in an array.

For example if array is: 1 9 4 5 8 3 1 3 5

The output should be 6, because 1,9,4,5,8,3 are uniqu

相关标签:
8条回答
  • 2021-02-04 20:34

    How about this?

    #include <list>
    
    int main()
    {
        int a[] = {1, 9, 4, 5, 8, 3, 1, 3, 5};
    
        std::list<int> la(a, a+9);
        la.sort();
        la.unique();
        std::cout << la.size() << std::endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-02-04 20:35

    this should work, however its probably not the optimum solution.

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    int a[50],n;        
    int uniqueNumbers; // this will be the total numbers entered and we will -- it
    cin >>n;    
    uniqueNumbers = n;  
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
    }   
    for (int j=0;j<n;j++)
    {   
        for(int k=0;k<n;k++)
        {
            /* 
            the and clause below is what I think you were missing.
            you were probably getting false positatives when j == k because a[1] will always == a[1] ;-)
            */
            if((a[k] == a[j]) && (k!=j)) 
            { uniqueNumebers--; }
        }       
    }
    cout << uniqueNumbers << endl;
    return 0;
    }
    
    0 讨论(0)
  • 2021-02-04 20:37

    Please dry run your code See in the outer for loop for each element it is counted more than one inside inner loop.let us say the loop contains 1,2,3,4.1.....elements dry run it in the second iteration and third iteration 1 is counted because 1 is 1!=2 as well as 1!=3

    Now solution time!!

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #define ll long long
    using namespace std;
    ll arr[1000007]={0};
    int main()
    {
      ios_base::sync_with_stdio(false);//used for fast i/o
        ll n;cin>>n;
          for(ll i=1;i<=n;i++)
            cin>>arr[i];
    
            sort(arr,arr+n);
    
           ll cnt=0;
                    for(ll i=1;i<=n-1;i++)
                      {
                       if(arr[i+1]-arr[i]==0)
                         cnt++;
                      }
                     cout<<n-cnt<<endl;
    
    
      cin.tie(NULL);
      return 0;
    }
    
    0 讨论(0)
  • 2021-02-04 20:40

    We can use C++ STL vector in this program .

      int main() 
      {
        int a[] = {1, 9, 4, 5, 8, 3, 1, 3, 5};
        vector<int>v(a, a+9);
    
        sort(v.begin(), v.end()); 
        v.erase(unique(v.begin(), v.end()), v.end()); 
    
        cout<<v.size()<<endl;
    
        return 0;
      }
    
    0 讨论(0)
  • 2021-02-04 20:42

    I think the location for increasing the value of r is incorrect

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int r=0,a[50],n;
        cin >>n;
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
        }
        for (int j=0;j<n;j++)
        {   
            bool flag = true;  
            for(int k=;k<j;k++)
            {
                if(a[k]!=a[j])
                {
                   flag = false;
                   break;
                }
           }
           if (true == flag) 
           {
               r++;
           }
        }
        cout << r << endl;
        return 0;
    }
    

    However, my suggestion is using more sophisticated algorithms (this algorithm has O(N^2)).

    0 讨论(0)
  • 2021-02-04 20:44

    A std::set contains only unique elements already.

    #include <set>
    
    int main()
    {
        int a[] = { 1, 9, 4, 5, 8, 3, 1, 3, 5 };
    
        std::set<int> sa(a, a + 9);
        std::cout << sa.size() << std::endl;
    }
    
    0 讨论(0)
提交回复
热议问题