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

What is io stream in java?

0 Answers  


Q1.A. Write note on “The class path Environment Variable”? B. Which are different kinds of source code? Q2.A. How to create an interface? B. Why convert an applet to an application? Q3.A. How to use Media tracker Class. B. How to use string tokenizer class. Q4 A. Explain the overview of UDP messaging. B. Difference between SQL Exception class and SQL Warning class. Q5. A. How to create com object in Java? B. Write short notes on “The properties class” Q6. A. When object is created and destroyed? B. Explain the JDB in depth & command line. C. Write short notes on Web Sites.

2 Answers   iGate, Seed Infotech,


Does substring start with 0?

0 Answers  


What are aggregate functions explain with examples?

0 Answers  


Which class should you use to obtain design information about an object in java programming?

0 Answers  






Explain about procedural programming language or structured programming language and its features?

0 Answers  


What is boolean example?

0 Answers  


Can classes declared using the abstract keyword cab be instantiated?

0 Answers   MCN Solutions,


Why java does not supports multiple inheritance?

3 Answers   TCS, VSoft,


Why do we create threads in java?

0 Answers  


What are the difference between string, string builder, and string buffer in java?

0 Answers  


What is api in java?

0 Answers  


Categories