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 |
Write a program to check for a prime number in java?
What is use of set in java?
What does it mean that a method or field is “static”?
What are the rules for naming an array?
how can i kill thread without stop() and destroy()
What is the default initialized value of a boolean type variable?
Name the methods that used to get and set the text label displayed by a Buttonobject?
What is flush () in java?
Can final class have constructor?
real time example for deadlock,starvation,livelock
Why runnable interface is used in java?
Is char a data type in java?