1

this is what I have. it is skipping over the zero. how can I fix that? I'm trying to count the number of times the numbers are duplicated.

void hit_rate(int a, int cmset[])
{
    int i, j, k=0;
    for(i=0;i<a;i++){
        for(j=i;j<a;j++){
            if((cmset[i] == cmset[j])){
                k++;
            }
        }
        printf("%d\n",k);
        k=0;
    }
}

cmset      k **now** prints  
  4          2       
  6          1        
  0          3       
  0          2       
  0          1       
  1          1       
  2          1       
  4          1    
1
  • 3
    for(j=0;j<=1;j++) is that correct? Commented Apr 11, 2012 at 5:00

2 Answers 2

3

While counting duplicates, e.g. arr[5] = {1, 2, 2, 3, 3}; start with

i = 0; // first loop
j = i; //2nd loop

comapre arr[i] == arr[j]; //condition By this what happens if you have tested arr[0] with all i = 1..4; in next iteration, you have need not to check a[1] with arr[0], because it's already done (or checked). increase the counter(when duplication matches). once it ends the end of the array reset counter. and print it. I hope it helps. Still confused then I will provide you sample code.

Sign up to request clarification or add additional context in comments.

Comments

3

Should be for(j=0;j<a;j++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.