Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method named \"hasNoDuplicates\" that takes an array of Strings a

ID: 3690799 • Letter: W

Question

Write a static method named "hasNoDuplicates" that takes an array of Strings as a parameter and that returns a boolean value indicating whether or not any of the Strings in the array are duplicated (true for yes, false for no).

For example, if a variable called list stores the following values:
     String[] list = {"a", "bb", "acb", "POP", "dad", "John", "no", "yes"};
Then the call of hasNoDuplicates(list) should return "true" because there are no duplicated values in this list.

If instead the list stored these values:
     String[] list = {"a", "bb", "acb", "POP", "dad", "John", "no", "yes", "no"};
Then the call should return "false" because the value "no" appears twice in this list. Notice that given this definition, a list of 0 or 1 elements would be considered unique.

public class HasDuplicates {
      public static void main(String[] args) {
            String[] list1 = {"a", "bb", "acb", "POP", "dad", "John", "no", "yes"};
            String[] list2 = {"a", "bb", "acb", "POP", "dad", "John", "no", "yes", "no"};


            System.out.println("List1 " + (hasNoDuplicates(list1)? "doesn't":"does")
                          + " contain duplicates");
            System.out.println("List2 " + (hasNoDuplicates(list2)? "doesn't":"does")
                          + " contain duplicates");
      }

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class HasDuplicates
{
   public static boolean hasNoDuplicates(String[] l){
       for(int i=0;i<l.length;i++){
           for(int j=i+1;j<l.length;j++){
               if(l[i].equals(l[j])){
               return false;
               }
           }
       }
       return true;
   }
public static void main(String[] args) {
String[] list1 = {"a", "bb", "acb", "POP", "dad", "John", "no", "yes"};
String[] list2 = {"a", "bb", "acb", "POP", "dad", "John", "no", "yes", "no"};

System.out.println("List1 " + (hasNoDuplicates(list1)? "doesn't":"does")
+ " contain duplicates");
System.out.println("List2 " + (hasNoDuplicates(list2)? "doesn't":"does")
+ " contain duplicates");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote