dcsimg
count duplicate elements in a list
6 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   java_learner
Posted On:   Wednesday, September 29, 2010 08:48 AM

Hi All,


I have a list which consists of duplicate elements

for ex: list {a,b,d,a,d,f,b}

How to count the number of occurrences of duplicate
elements in a list ?


Thanks in Advance

Re: count duplicate elements in a list

Posted By:   rahul123  
Posted On:   Tuesday, December 17, 2013 09:01 PM

This code can help you


import java.util.*;
public class  LL
{
	public static void main(String[] args)
	{

	   List ll=new LinkedList();
	   ll.add(new Character('a'));
	   ll.add(new Character('b'));
	   ll.add(new Character('d'));
	   ll.add(new Character('a'));
	   ll.add(new Character('d'));
	   ll.add(new Character('f'));
           ll.add(new Character('b'));
	   System.out.println("Content ="+ll);
       System.out.println("Size    ="+ll.size());



        int a=0;
	   ListIterator litr1=ll.listIterator();
	   ListIterator litr2=ll.listIterator();

	   while(litr1.hasNext())
		{
		   Object obj1=litr1.next();
		   Character str1=(Character)obj1;
		   char ch1=str1.charValue();

		   while(litr2.hasNext())
		{
		   Object obj2=litr2.next();
		   Character str2=(Character)obj2;
		   char ch2=str2.charValue();

		   if(ch1==ch2)
			{

			   a++;
			}
		}
		System.out.println(ch1+" is repeated in "+a+" Times");
		a=0;
		while(litr2.hasPrevious())
			{
			Object o=litr2.previous();
			}


		}

	}
}

Re: count duplicate elements in a list

Posted By:   bbuggins  
Posted On:   Friday, June 14, 2013 05:39 PM

Limy Dedicated Servers  are the ultimate hosting solution that the ever-changing online industry can offer at the moment. With total authority over your own server and its excellent response time, your site will grow together with your business and you will keep your customers highly satisfied. You do not have to worry about sharing your resources – you get an entire server with all the space and bandwidth at your disposal.
http://www.limyservers.com/

Re: count duplicate elements in a list

Posted By:   aroon7132  
Posted On:   Thursday, May 23, 2013 05:42 AM

Hi,

This code can help You

public class BasicMapProgram {

    public static void main(String[] args) {

        List<String> listString = new ArrayList<String>();

        listString.add("arun");
        listString.add("lokesh");
        listString.add("hemanth");
        listString.add("hemanth");

        Map<String, Integer> map = new HashMap<String, Integer>();

        Integer integer = new Integer(1);
        for (String string : listString) {
            if (!map.containsKey(string)) {
                map.put(string, integer);
            } else {
                Set<Entry<String, Integer>> keySet = map.entrySet();
                for (Entry<String, Integer> entry : keySet) {
                    if (entry.getKey().equalsIgnoreCase(string)) {
                        map.put(string, entry.getValue() + 1);
                    }
                }

            }
        }

        Set<Entry<String, Integer>> outerSet = map.entrySet();
        for (Entry<String, Integer> entry : outerSet) {
            System.out.println(entry.getKey() + " Value is  "
                    + entry.getValue());
        }
    }

}

Re: count duplicate elements in a list

Posted By:   amitg  
Posted On:   Monday, August 6, 2012 08:41 PM

better you read the size of list and then put all the element in set and again read the size and do the delta which is nothing but your count of duplicate element

count duplicate elements in a list

Posted By:   G_Muraliraj  
Posted On:   Monday, November 22, 2010 02:50 AM

 Hi I think it is usefull to u.. Thank you 



import java.util.*;
public class LL
{
public static void main(String[] args)
{

List ll=new LinkedList();
ll.add(new Character('a'));
ll.add(new Character('b'));
ll.add(new Character('d'));
ll.add(new Character('a'));
ll.add(new Character('d'));
ll.add(new Character('f'));
ll.add(new Character('b'));
System.out.println("Content ="+ll);
System.out.println("Size ="+ll.size());



int a=0;
ListIterator litr1=ll.listIterator();
ListIterator litr2=ll.listIterator();

while(litr1.hasNext())
{
Object obj1=litr1.next();
Character str1=(Character)obj1;
char ch1=str1.charValue();

while(litr2.hasNext())
{
Object obj2=litr2.next();
Character str2=(Character)obj2;
char ch2=str2.charValue();

if(ch1==ch2)
{

a++;
}
}
System.out.println(ch1+" is repeated in "+a+" Times");
a=0;
while(litr2.hasPrevious())
{
Object o=litr2.previous();
}


}

}
}

Re: count duplicate elements in a list

Posted By:   Anonymous  
Posted On:   Tuesday, November 9, 2010 07:19 AM

hi
About | Sitemap | Contact