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



How to count occurrences of each duplicate element in a list {a,b,d,c,a,b} ? Thanks in Advance..

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

How to count occurrences of each duplicate element in a list {a,b,d,c,a,b} ? Thanks in Advance..

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

Post New Answer

More Core Java Interview Questions

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

7 Answers  


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?

4 Answers   Satyam,


What is abstract class constructor called?

0 Answers  


What is collection class in java? List down its methods and interfaces.

0 Answers  


What is user defined exception in Java?

0 Answers   TCS,






what are the boundaries of jdk and jre?

0 Answers   CoreObjects,


A person says that he compiled a java class successfully without even having a main method in it? Is it possible?

0 Answers  


If your team member writes code with lots of static variables and static methods, will it cause any side effects?

3 Answers   KPIT,


What is string immutability?

0 Answers  


What is difference between an object and a class?

0 Answers   Amdocs,


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?

0 Answers  


Categories