Write a program using c++ named isUnique that takes an array of integers as a pa
ID: 3596531 • Letter: W
Question
Write a program using c++ named isUnique that takes an array of integers as a parameter and that returns a boolean value indicating whether or not the values in the array are unique (true for yes, false for no). The values in the list are considered unique if there is no pair of values that are equal. For example, if a variable called list stores the following values:
int list[14] = {3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97, 10, 4};
Then the call of isUnique(list) should return true because there are no duplicated values in this list.
If instead the list stored these values:
int[11] list = {4, 7, 2, 3, 9, 12, -47, -19, 308, 3, 74};
Then the call should return false because the value 3 appears twice in this list. Notice that given this definition, a list of 0 or 1 elements would be considered unique.
Explanation / Answer
#include <iostream>
using namespace std;
bool isUnique(int a[], int size){
for(int i=0;i<size;i++) {
for(int j=i+1;j<size;j++) {
if(a[i]==a[j+1]) {
return false;
}
}
}
return true;
}
int main()
{
int list1[14] = {3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97, 10, 4};
cout<<isUnique(list1, 14)<<endl;
int list2[11] = {4, 7, 2, 3, 9, 12, -47, -19, 308, 3, 74};
cout<<isUnique(list2, 11)<<endl;
return 0;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.