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?

Answers were Sorted based on User's Feedback



I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / s.ramesh

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 ?    14 Yes 1 No

I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / srivatsava

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 ?    8 Yes 0 No

I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / gopal

Hey sorry. There is a small correction to the above code

/**
Remove duplicates from ArrayList<String> without using Set
*/
private static void removeDuplicates (ArrayList<String> al) {
for (int i=0;i <al.size(); i++) {
int index = al.lastIndexOf(al.get(i));
while (index != -1 && index != i) {
al.remove(i);
index = al.lastIndexOf(al.get(i));
}
}
}

Is This Answer Correct ?    2 Yes 0 No

I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / athira

package com.modon;

import java.util.ArrayList;

public class MyArrayList {

public static void main(String[] args) {

ArrayList<Object> dupList=new ArrayList<Object>();

ArrayList<Object> resultList=new ArrayList<Object>();
dupList.add(1);
dupList.add(2);
dupList.add(3);
dupList.add("D");
dupList.add("A");
dupList.add("F");
dupList.add("A");
dupList.add("A");
dupList.add(1.5);
dupList.add(1.50);
dupList.add(new String("A"));
dupList.add(new Integer(3));

for(Object s:dupList){

if(!resultList.contains(s))
resultList.add(s);
}

System.out.println("dupList: "+dupList.size());

System.out.println("resultList: "+resultList.size());

}

}

Is This Answer Correct ?    2 Yes 0 No

I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / sreenivas

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

I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / gopal

Guys lets keep it simple:

/**
Remove duplicates from ArrayList<String> without using Set
*/
private static void removeDuplicates (ArrayList<String> al) {
for (int i=0;i <al.size(); i++) {
int index = al.lastIndexOf(al.get(i));
if (index != -1 && index != i) {
al.remove(i);
index = al.lastIndexOf(al.get(i));
}
}
}

Is This Answer Correct ?    1 Yes 0 No

I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / john

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 ?    7 Yes 10 No

I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate da..

Answer / murli

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 ?    2 Yes 5 No

Post New Answer

More Core Java Interview Questions

What is java oops?

0 Answers  


Does java list allow null?

0 Answers  


What is difference between ++ I and I ++ in java?

0 Answers  


What is the use of default method in interface in java?

0 Answers  


what is the main difference between string and stringbuffer? can you explain it with program?

2 Answers  






Can Exception handling we can handle multiple catch blocks?

0 Answers   PUCIT,


Is it possible to use string in the switch case?

0 Answers  


Is array a class?

0 Answers  


How many threads can I run java?

0 Answers  


How do you sort a string in alphabetical order in java?

0 Answers  


os is developed in c no java is more secured then c na why dont the os developed is developed using java

1 Answers  


What is a dynamic array java?

0 Answers  


Categories