In: Computer Science
Create a static method with the appropriate inputs and outputs. Call each of them in the main method.
Write a method called unique() which takes in a List and returns true if all the items in the List are unique. All the items are unique if none of them are the same.Return false otherwise.
Please do this in Java.
import java.util.ArrayList; import java.util.List; public class UniqiueCheck { public static boolean unique(List list){ for(int i = 0;i<list.size();i++){ for(int j = i+1;j<list.size();j++){ if(list.get(i).equals(list.get(j))){ return false; } } } return true; } public static void main(String[] args) { List<Integer> list1 = new ArrayList<>(); list1.add(1); list1.add(2); list1.add(3); list1.add(4); System.out.println(unique(list1)); List<Integer> list2 = new ArrayList<>(); list2.add(1); list2.add(2); list2.add(1); list2.add(4); System.out.println(unique(list2)); } }
true false
public static boolean unique(List list){ for(int i = 0;i<list.size();i++){ for(int j = i+1;j<list.size();j++){ if(list.get(i).equals(list.get(j))){ return false; } } } return true; }