You are given two int variables j and k, an int array zipcodeList that has been declared and initialized, an int variable nZips that contains the number of elements in zipcodeList, and a bool variable duplicates. Write some code that assigns true to duplicates if any two elements in the array have the same value, and that assigns false to duplicates otherwise. Use only j, k, zipcodeList, nZips, and duplicates.

LANGUAGE: C++

CHALLENGE:

You are given two int variables j and k, an int array zipcodeList that has been declared and initialized, an int variable nZips that contains the number of elements in zipcodeList, and a bool variable duplicates. Write some code that assigns true to duplicates if any two elements in the array have the same value, and that assigns false to duplicates otherwise. Use only j, k, zipcodeList, nZips, and duplicates.

SOLUTION:


duplicates = false;
for (k =0; k < nZips; k++){
    for(j=k+1 ;j<nZips;j++){
        if (zipcodeList[k] == zipcodeList[j]){
            duplicates = true;
            break;
        }
    }
}