Find unique numbers in array

后端 未结 8 1138
被撕碎了的回忆
被撕碎了的回忆 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: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
    #include
    #include
    #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<

提交回复
热议问题