(1) You are sorting the vector so duplicate values will be consecutively present in the vector.
(2) Now we come to the logic behind that condition:
- As the vector can contain the duplicates we should make sure to not theto repeat the same sequence by starting with the same element again(duplicate)
- example :
1,1,2(starting withfirstone) and 1,1,2 (starting withsecondone) => both are same (we should make sure this doesn't happen)
- example :
Example: In [1, 1, 2]
If the first element in the sequence is chosen as 1, then we should make sure we are not going to create another sequence starting with another 1 (duplicate of the element).
So for that, we need to skip the loop for all the consecutive duplicate elements.
In youyour case, after creating a sequence staring with first 1, when we enter the loop for the second time we check whether the current element is a duplicate
nums[i] == nums[i - 1](this is sufficient as you have already sorted the vector) and whether the current element is the first element of the sequence!used[i - 1].- In your case, due to this condition, the second one cannot be the first element of the sequence.