Following is the code snippet I am working with.
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<StringBuffer>();
while (N-- > 0) {
str = new StringBuffer(sc.next());
if (al.contains(str)) {
System.out.println("Duplicate value " + str);
} else {
al.add(str);
}
}
If the input is: 4
abc
fgh
dfg
abc
It is showing blank output when the expected output is:
Duplicate value abc
Where am I going wrong here?
StringBuffer
doesn't override Object
's equals
, so when you search if your List
contains a certain StringBuffer
instance, you are checking if the exact reference appears in the List
.
You could use a HashSet<String>
to avoid duplicates, since String
overrides equals
, and then (if you must) create a List<StringBuffer>
from the elements of the HashSet
.
BTW, StringBuilder
is more efficient than StringBuffer
(which should only be used if you plan to access it from multiple threads).
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<StringBuffer>();
Set<String> uniques = new HashSet<>();
while (N-- > 0) {
uniques.add(sc.next());
}
for (String s : uniques)
al.add (new StringBuffer(s));
If you have to report the duplicates, a small change will be required :
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<StringBuffer>();
Set<String> uniques = new HashSet<>();
while (N-- > 0) {
String str = sc.next();
if (!uniques.add(str))
System.out.println("Duplicate value " + str);
}
for (String s : uniques)
al.add (new StringBuffer(s));
You can edit your code as below:
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<>();
while (N-- > 0) {
str = new StringBuffer(sc.next());
//if (al.contains(str)) {
if (al.stream().anyMatch(stringBuffer -> stringBuffer.toString().equals(str.toString()))) {
System.out.println("Duplicate value " + str);
} else {
al.add(str);
}
}
In code above
if (al.contains(str)) {
Changed to
if (al.stream().anyMatch(stringBuffer -> stringBuffer.toString().equals(str.toString()))) {
I am here posting the workaround to the problem that I was facing.
The actual problem statement is here .
The following is the code snippet of one of the ways which I implemented to solve the problem statement, using linkedlist:
import java.util.*;
public class Password {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String str;
LinkedList ll = new LinkedList();
StringBuilder sb = new StringBuilder();
for (int i=0; i<N; i++) {
str = sc.next();
ll.add(str);
}
for (int i=0; i<N; i++) {
str = (sb.append(ll.get(i))).reverse().toString();
if (ll.contains(str)) {
System.out.print(str.length() + " " + str.charAt(str.length()/2));
break;
}
sb.setLength(0);
}
sc.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.