Help me in solving CNDY problem

My issue

include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here

int t,n,i,x,count=0;
cin>>t;
while(t--){
    cin>>n;
    int a[2*n];
    for(i=0;i<2*n;i++){
        cin>>a[i];
    }
    for(x=0;x<2*n;x++){
        for(i=1;i<2*x;i++){
            if(a[x]==a[i]){
                count++;
            }
            i=i+1;
        }
    }
    if(count>=2){
        cout<<"NO"<<endl;
    }
    else{
        cout<<"YES"<<endl;
    }
    
    
}

}

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	
	int t,n,i,x,count=0;
	cin>>t;
	while(t--){
	    cin>>n;
	    int a[2*n];
	    for(i=0;i<2*n;i++){
	        cin>>a[i];
	    }
	    for(x=0;x<2*n;x++){
	        for(i=1;i<2*x;i++){
	            if(a[x]==a[i]){
	                count++;
	            }
	            i=i+1;
	        }
	    }
	    if(count>=2){
	        cout<<"NO"<<endl;
	    }
	    else{
	        cout<<"YES"<<endl;
	    }
	    
	    
	}

}

Problem Link: Candies Practice Coding Problem

No you are doing wrong in counting no. of time a variable appear
you can sort the arrray then find if there are any integer that appeared more than twice