Write a method named isUnique that takes an array of integers as a parameter and
ID: 3658603 • Letter: W
Question
Write a 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: int[] list = {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[] 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
please rate-thanks
import java.util.*;
public class main
{
public static void main(String[] args)
{Scanner in = new Scanner(System.in);
int[] list1 = {3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97, 10, 4};
int[] list2 = {4, 7, 2, 3, 9, 12, -47, -19, 308, 3, 74};
int[] list3={1};
int []list4={};
if(isUnique(list1))
System.out.println("list 1 is unique");
else
System.out.println("list 1 is not unique");
if(isUnique(list2))
System.out.println("list 2 is unique");
else
System.out.println("list 2 is not unique");
if(isUnique(list3))
System.out.println("list 3 is unique");
else
System.out.println("list 3 is not unique");
if(isUnique(list4))
System.out.println("list 4 is unique");
else
System.out.println("list 4 is not unique");
}
public static boolean isUnique(int[] list)
{int i,j;
for(i=0;i<list.length-1;i++)
for(j=i+1;j<list.length;j++)
if(list[i]==list[j])
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.