𝔉𝓮𝓵𝓲𝓬𝓲𝓪 𝓗𝓪𝓰𝓰𝓪𝓻𝓭
𝔉𝓮𝓵𝓲𝓬𝓲𝓪 𝓗𝓪𝓰𝓰𝓪𝓻𝓭
CC#
Created by 𝔉𝓮𝓵𝓲𝓬𝓲𝓪 𝓗𝓪𝓰𝓰𝓪𝓻𝓭 on 6/17/2023 in #help
❔ Verify Uniqueness in an Array
Does anyone know how to determine if an array contain unique names in an array without using previously defined methods. I have worked on this for days and finally just had to submit the homework without figuring it out. The instructions: "Given an array of string, names, verify that each name is unique meaning that none of the names are duplicated within the array. If the array is unique, return true; otherwise, return false." My code:
public static bool Test4(string [] names)
{
bool unique = false;
for (int i = 0; i < names.Length; i++)
{
for(int j = 1; j < i; j++)
{
if (names[i] != names[j] && names[j] != names[i])
{
unique = true;

}
else if (names[i] == names[j] && names[j] == names[i])
{
unique = false;
}
}
}
return unique;
}
public static bool Test4(string [] names)
{
bool unique = false;
for (int i = 0; i < names.Length; i++)
{
for(int j = 1; j < i; j++)
{
if (names[i] != names[j] && names[j] != names[i])
{
unique = true;

}
else if (names[i] == names[j] && names[j] == names[i])
{
unique = false;
}
}
}
return unique;
}
13 replies