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
int numWords = stringsArray.length;
for(int i=0;i for(int j=i+1; j
if(stringsArray[i].equals(stringsArray[j])){
return true;
}
}
}
return false;
}
}
// Please use a different method for the duplicates other than the one above.
Thanks.
import java.util.HashSet; 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) { HashSet<String> set = new HashSet<String>(); for (int i = 0; i < stringsArray.length; i++) { if (set.contains(stringsArray[i])) return true; set.add(stringsArray[i]); } return false; } }
Get Answers For Free
Most questions answered within 1 hours.