C programming You are given two int variables j and k an int
(C programming)
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 an int variable duplicates.
Write some code that assigns 1 to duplicates if any two elements in the array have the same value , and that assigns 0 to duplicates otherwise. Use only j, k, zipcodeList, nZips, and duplicates.
Solution
Here is the C function for your requirement:
#include <stdio.h>
int hasDuplicates(int zipcodeList[], int nZips)
{
int duplicates = 0;
for(int i = 0; i < nZips - 1; i++)
for(int j = i+1; j < nZips; j++)
if(zipcodeList[i] == zipcodeList[j])
{
duplicates = 1;
return duplicates;
}
return duplicates;
}
