ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
I have a Arraylist object, it has duplecate values also. Now
question is i want delete duplecate data in that objet with
out using Set?
 Question Submitted By :: Seenu
I also faced this Question!!     Rank Answer Posted By  
 
  Re: I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate data in that objet with out using Set?
Answer
# 1
ArrayList al = new ArrayList();

al.add("A");
al.add("B");
al.add("A");
al.add("C');

for(int i = 0; i<al.size(); i++)
      System.out.println(": " + al.get(i));

if(al.contains("A"))
      al.remove("A");

for(int i = 0; i<al.size(); i++)
      System.out.println("- " + al.get(i));
 
Is This Answer Correct ?    3 Yes 2 No
John
 
  Re: I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate data in that objet with out using Set?
Answer
# 2
solution without taking time complexity into account

List arr = new ArrayList();
for(int i=0;i<count;i++){
			for(int j=i+1;j<count;j++){
				if(arr.get(i)!=null && arr.get(i).equals(arr.get(j))){
					arr.set(j, null);
				}
					
			}
		}

while(arr.contains(null))
			arr.remove(null);
 
Is This Answer Correct ?    0 Yes 2 No
Murli
 
 
 
  Re: I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate data in that objet with out using Set?
Answer
# 3
import java.util.ArrayList;

public class DemoTest
{
	public static void main (String args[])
	{
		ArrayList arrList = new ArrayList();
		arrList.add("One");
		arrList.add("Two");
		arrList.add("Two");
		arrList.add("Three");
		arrList.add("Four");
		arrList.add("One");
		arrList.add("Four");
		arrList.add("One");
		arrList.add("Six");
		arrList.add("One");
		arrList.add("Ten");
		arrList.add("Three");
		arrList.add("One");
		arrList.add("Two");
		arrList.add("One");
		arrList.add("One");
		for(int i=0;i<arrList.size();i++)
		{
			String ele = (String) arrList.get(i);
			boolean f = true;
			while(f)
			{
				int firstInd = arrList.indexOf(ele);
				int lastInd = arrList.lastIndexOf(ele);
				if(firstInd==lastInd)
				{
					f=false;
				}
				else
				{
					arrList.remove(lastInd);
				}
			}
		}
		for(int i=0;i<arrList.size();i++)
			System.out.println((String)arrList.get(i));
	}	
}
 
Is This Answer Correct ?    6 Yes 1 No
S.ramesh
 
  Re: I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate data in that objet with out using Set?
Answer
# 4
Hi,
I hope the answer I am providing is the easiest one among 
all others

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListDemo
{
	public static void main(String[] args) 
	{
		ArrayList containsDuplicate = new ArrayList
();

		containsDuplicate.add("A");
		containsDuplicate.add("B");
		containsDuplicate.add("C");
		containsDuplicate.add("D");
		containsDuplicate.add("A");
		containsDuplicate.add("B");
		containsDuplicate.add("C");
		containsDuplicate.add("A");
		
		ArrayList noDuplicate = new ArrayList
(containsDuplicate);
		ArrayList fresh = new ArrayList();

		System.out.println("Hello World!!!!!!!! 
with duplicates " + containsDuplicate.size());
		System.out.println("Hello World!!!!!!!! 
with duplicates " + noDuplicate.size());
		for(int i =0;i<containsDuplicate.size();i++)
		{
			String outerObject = (String) 
containsDuplicate.get(i);
			int count = 0;
			for(int j=0;j<noDuplicate.size
();j++)
			{
				String innerObject = 
(String) noDuplicate.get(j);				
				if(outerObject.equals
(innerObject))
				{
					if((count == 0))
					{
						count++;
						// fresh.add
(outerObject);
					}
					else
					{
					
	noDuplicate.remove(j);				
		
					}
				}			
	
			}			
		}

		for(int i=0; i< noDuplicate.size();i++)
		{
			System.out.println("Each 
Element :::::::: " + noDuplicate.get(i));
		}		
	}
}
 
Is This Answer Correct ?    1 Yes 0 No
Sreenivas
 
  Re: I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate data in that objet with out using Set?
Answer
# 5
import java.util.*;
public class ArrayListDuplicateValueDeleteExample {

	/**
	 * ArraList Duplicate values removed without using Set.
	 */
	public static void main(String[] args) {
		ArrayListDuplicateValueDeleteExample obj = new ArrayListDuplicateValueDeleteExample();
		ArrayList al = new ArrayList();
		ArrayList al1 = new ArrayList();
		al.add("A");
		al.add("B");
		al.add("B");
		al.add("B");
		al.add("B");
		al.add("C");
		al.add("A");
		al.add("A");
		al.add("A");
		al.add("A");
		al.add("A");
		System.out.println("Size of the array  -  "+al.size());
		System.out.println("ArrayList Values with Duplicate -  "+al);

		for(int i=0;i<al.size();i++){
			if(al.contains(al.get(i))){
				if (al1.contains(al.get(i))){
				
				}else {
					al1.add(al.get(i));
				}
			}
		}
		System.out.println("New ArrayList Values without Duplicate -  "+al1);
}
}
 
Is This Answer Correct ?    1 Yes 0 No
Srivatsava
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
What is the difference between C++ & Java? Syntel21
AWT event listeners extends what interface?  1
Inorder to specify a container?s layout, which method is used?  1
Name the types of mouse event listeners ?  2
What is the return type of a program?s main() method?  2
what is the use/perpose of having a method antive?  2
What is Hash Code in Java? Cognizent1
What is an Iterator interface?  2
Can an exception be rethrown? Wipro4
Is &&= a valid Java operator?  2
What is exception and error? and what is the difference between them?  2
What is operator?  2
Difference between prefix and postfix forms of the ++operator?  2
What is Exception handling in Java How do you handle run time errors please explain with an example TCS2
How are Java source code files named?  3
How to access a variable if it is declared as private?  2
What classes of exceptions, caught by a catch clause?  1
what is Thread?  6
StringBuilder s = new StringBuilder("Hello Mom");s.append(",I mean,Mother"); Response.Write(s.ToString()); String s = "Hello Mom"; s+ = ",I mean Mom"; Response.Write(s); Which is faster ? which uses the most memory?  4
Name the class that used to read objects directly from a stream? Wipro2
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com