How to count occurrences of each duplicate element in a list
{a,b,d,c,a,b} ?
Thanks in Advance
Answers were Sorted based on User's Feedback
Answer / lal ajith kumara
Since java.Util.Set is not allowing duplicates u can use it
String[] letters = {"a", "b", "b", "c" ,"a", "d","a"};
Set duplicateTester = new HashSet<String>();
for(int i = 0; i<letters.length;i++){
if(true == duplicateTester.add(letters[i])){
System.out.println("true>>"+letters[i]);
}else{
System.out.println("false>>"+letters[i]);
}
}
Is This Answer Correct ? | 7 Yes | 1 No |
Answer / pratiksha
import java.util.*;
class alpha
{
String let;
public alpha(String l)
{
this.let=l;
}
}
public class Duplicateletters
{
public static void main(String []args)
{
int i=0;
String[] letters = {"a", "b", "b", "c" ,"a", "d","a"};
ArrayList<alpha> a = new ArrayList<alpha>();
for(i=0;i<letters.length;i++)
{
a.add(new alpha(letters[i]));
}
String let1=null;
Iterator<alpha> i1 = a.iterator();
while(i1.hasNext())
{
alpha a1 = i1.next();
let1=a1.let;
}
int j=0;
Iterator<alpha> i2 = a.iterator();
while(i2.hasNext())
{
alpha a2 = i2.next();
if(let1==a2.let)
{
j=j+1;
}
}
System.out.println(j);
}
}
Is This Answer Correct ? | 3 Yes | 1 No |
Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want to count the letters how many times it occures.i.e from the above string the out put should be like this U-1,N-2,D-2,E-1,R-1,S-1,T-1,A-1. how can i achieve this Thnaks in advance for your response ..
Can you explain the difference b/n abtract and interface with a good example,?In what cases we have use abtract and what case interface?
What is abstract class constructor called?
What is collection class in java? List down its methods and interfaces.
What is user defined exception in Java?
what are the boundaries of jdk and jre?
A person says that he compiled a java class successfully without even having a main method in it? Is it possible?
If your team member writes code with lots of static variables and static methods, will it cause any side effects?
What is string immutability?
What is difference between an object and a class?
what is difference Between Core Java and advance java
60 Answers HCL, HP, Sambalpur University, TCS, Vensai Technologies, Wipro,
Is it necessary that each try block must be followed by a catch block?