Write a static method named isUnique that takes an array of integers as a parame
ID: 3540501 • Letter: W
Question
Write a static method 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:
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:
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
public class driverclass {
public static void main(String[] args){
int[] list = {3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97};
boolean y = isUnique(list);
System.out.println("True if list is unique :");
System.out.println("False if list is not unique :");
System.out.println(y);
}
private static boolean isUnique(int[] list) {
boolean x = true;
if(list.length==0 || list.length==1){
x = true;
}
for(int i=0;i<list.length;i++){
for(int j=0;j<list.length;j++){
if(list[i]==list[j] && i!=j){
x =false;
break;
}
}
}
return x;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.