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...

  • 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     } }

  • 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...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • 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...

  • 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...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • 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,...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

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
ADVERTISEMENT