我无法弄清楚这个循环是如何不中断并给出正确的值的。它只是不断增加

I can't figure out how this loop not breaking and give the right value. It just keeps adding up

Code to bring copy the unique value from array A to B. And return the number of unique value which is the number of element in B

#include <stdio.h>
int func(int A[], int B[], int n)
{
 int i, j, inc = 0;
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < i; j++)
        {
            if(A[i] == A[j])
            
                break;

this break supposed to break out the inner loop incrementally

        if(i == j)
        {
            B[inc] = A[i];
            inc++;
        }
        }
     
    }
  
 
}
int main()
{
    int A[100] = {1, 3, 4, 5, 3, 2, 5, 4, 1, 0};
    int B[100];
    int c;
    c = func(A, B, 10);
    printf(" %d", c);
}

您对 ij 的测试放错了地方。它属于外部,但在内部循环之后。当然,您需要 return inc.

#include <stdio.h>
int func(int A[], int B[], int n)
{
    int i, j, inc = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < i; j++)
        {
            if (A[i] == A[j])
                break;
        }
        if (i == j)
        {
            B[inc] = A[i];
            inc++;
        }
    }
    return inc;
}

int main()
{
    int A[100] = {1, 3, 4, 5, 3, 2, 5, 4, 1, 0};
    int B[100];
    int c;
    c = func(A, B, 10);
    printf("%d\n", c);
    for (int i=0; i<c; ++i)
        printf("%d ", B[i]);
    fputc('\n', stdout);
}

输出

6
1 3 4 5 2 0 

这是正确答案。源数组中有六个唯一值,包括 0...5。