Question

Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not.

Your code should work well on long lists of strings.

Use the following set-up to write the code in JAVA

import java.lang.UnsupportedOperationException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
// parse the number of strings
int numStrings = Integer.parseInt(sc.nextLine());
  
// parse each string
String[] stringsArray = new String[numStrings];
for (int i = 0; i < numStrings; i++) {
stringsArray[i] = sc.nextLine();
}
  
// print whether there are duplicates
System.out.println(hasDuplicates(stringsArray));
}
  
private static boolean hasDuplicates(String[] stringsArray) {
// TODO fill this in and remove the below line
throw new UnsupportedOperationException();
}
}

ONLY USE THIS FOLLOWING SET UP ON THE TOP TO WRITE THE CIDE IN JAVA...

(show an example for the input and out of the code when finished as well)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.lang.UnsupportedOperationException;
import java.util.Scanner;

public class HasDuplicates {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

// parse the number of strings
        int numStrings = Integer.parseInt(sc.nextLine());

// parse each string
        String[] stringsArray = new String[numStrings];
        for (int i = 0; i < numStrings; i++) {
            stringsArray[i] = sc.nextLine();
        }

// print whether there are duplicates
        System.out.println(hasDuplicates(stringsArray));
    }

    private static boolean hasDuplicates(String[] stringsArray) {
        try {
            int numWords = stringsArray.length;
            for (int i = 0; i < numWords; i++) {
                for (int j = i + 1; j < numWords; j++) {
                    if (stringsArray[i].equals(stringsArray[j])) {
                        return true;
                    }
                }
            }
            return false;
        }
        catch (Exception e){
            throw new UnsupportedOperationException();
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The prime factorization of a number is the unique list of prime numbers that, when multiplied,...

    The prime factorization of a number is the unique list of prime numbers that, when multiplied, gives the number. For example, the prime factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5. In this problem you must write code to recursively find and return the prime factorization of the given number. You must print these in ascending sorted order with spaces in between. For example, if your input is: 120 then you should print the following output: 2...

  • I need help with my Java code. A user enters a sentence and the program will...

    I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many times. If the same word is in the sentence twice, but one is capitalized, it will not count it as a duplicate. How can I make the program read the same word as the same word, even if one is capitalized and the other is not? For example, "the" and "The" should be counted as...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • The program prompts the user to input three words, then display them in      sorted order....

    The program prompts the user to input three words, then display them in      sorted order. Strings have to be compared with compareTo method. */ import java.util.Scanner; public class Lab4Part13{     public static void main(String[] args){         Scanner input = new Scanner(System.in);         // Write rest of the code here     } }

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Java Your boss has just put you in charge of updating the company's client contact list....

    Java Your boss has just put you in charge of updating the company's client contact list. This file contains records in the following format: id,first_name,last_name,email 1,Norry,Killby,nkillby0@photobucket.com Your job is to reformat each record as follows: last_name, first_name, email Killby, Norry, <nkillby0@photobucket.com> Starter Code: package contactlistupdater; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.Formatter; import java.util.Scanner; import sun.reflect.generics.reflectiveObjects.NotImplementedException; public class ContactListUpdater { private static final String INPUT_FILENAME = "contacts.txt"; private static final String OUTPUT_FILENAME = "updated-contacts.txt"; /** * Transforms a String...

  • 7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the...

    7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the quicksort() and partition() methods to sort the IDs in ascending order using the Quicksort algorithm, and output the sorted IDs one per line. Ex. If the input is: kaylasimms julia myron1994 kaylajones -1 the output is: julia kaylajones kaylasimms myron1994 Code: import java.util.Scanner; import java.util.ArrayList; public class UserIDSorting { // TODO: Write the partitioning algorithm - pick the middle element as the // pivot,...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Write in Java Implement the parse method and test it by calling with three different strings...

    Write in Java Implement the parse method and test it by calling with three different strings and by printing the results. The Scanner method can be used to read values from strings, files, or System.in. We need to invoke the useDelimiter method to define what symbols can separate, or terminate, the digits of a Fraction. public static Fraction parse(String input) t Scanner s new Scanner(input) useDelimitercTVMitln"); int num s.nextlnt() int denom s.nextlnt); s.close): return new Fraction(num, denom) class Codechef static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT