enter image description hereCan anyone tell me why we should set the counter j in the inner for loop to (i+1) not (i=0) (beacause I think setting j to i = 0 will get all elements in the array). And why we using break; I am new in programming. Thanks in advance.
Here I check for the duplicates and if I set (j = i+1) the first element in the array will be skipped (with the index 0) from the comparison isn't it ?
So when checking for duplicates in an array (that's what I think the algorithm is for; correct me if I'm wrong), you want to check each element against each other element once. Let's have a 2D array of all possible pairs of elements you can check.
For an array with 4 elements, the 2D array would look like this:
We can traverse this 2D array like you said.
However, this checks both (1,0) and (0,1) which is redundant. All we have to do is check the elements above and including the diagonal from the top left to the bottom right. This makes it so that we have to set j to i+1.