package com.javainhouse;
import java.util.ArrayList;
public class RemoveDuplicates {
public static void main(String[] args)
{
ArrayList<Object> al = new ArrayList<Object>();
al.add("java");
al.add('a');
al.add('b');
al.add('a');
al.add("java");
al.add(10.3);
al.add('c');
al.add(14);
al.add("java");
al.add(12);
System.out.println("Before Remove Duplicate elements:"+al);
for(int i=0;i<al.size();i++)
{
for(int j=i+1;j<al.size();j++)
{
if(al.get(i).equals(al.get(j)))
{
al.remove(j); j--;
}
}
}
System.out.println("After Removing duplicate elements:"+al);
}
}
|
Home » Posts filed under arrayList
Java Program To Remove Duplicate Elements From ArrayList without using Collections
in
arrayList,
collection,
elements,
Java,
java program,
java programming,
remove
- on 11:47:00
- No comments
How to Find duplicate numbers in given list in Java
in
arrayList,
Coding Problems,
Java,
java program,
java programming,
List
- on 10:46:00
- No comments
For this program we are going to use HashSet as Set does not allow duplicate elements.
We will be using two HashSets . As he list is traversed ,all the elements will be started storing in the first hash set and if any element is traversed again the first hash set will not allow that element and that element will be stored in the second hash set and at last we ll print the second Hash set .
We will be using two HashSets . As he list is traversed ,all the elements will be started storing in the first hash set and if any element is traversed again the first hash set will not allow that element and that element will be stored in the second hash set and at last we ll print the second Hash set .
package com.javainhouse;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class findDuplicateElements
{
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
for (int i = 0; i < 10; i++) {
list.add(String.valueOf(i));
}
for (int i = 2; i < 7; i++) {
list.add(String.valueOf(i));
}
final Set<String> setToReturn = new HashSet<String>();
final Set<String> set1 = new HashSet<String>();
for (String element : list) {
if (!set1.add(element)) {
setToReturn.add(element);
}
}
System.out.println("Original List : " + list);
System.out.println("Duplicate elements from list : " + setToReturn);
}
}
|
OutPut :
| Duplicate Elements in List Program |
How to sort and reverse an array list without using sort method
in
arrayList,
Coding Problems,
Java,
java program,
Reverse of array list,
sorting of ArrayList,
Tutorial
- on 01:59:00
- No comments
package test;
import java.util.ArrayList;
import java.util.List;
public class testing {
public static void main(String[] args)
{ List<Integer> l= new ArrayList<Integer>();
l.add(1);
l.add(7); l.add(90); l.add(67); int temp; for (int i = 0; i < l.size(); i++) { for (int j =0 ; j < l.size(); j++) {
if(l.get(i)>l.get(j))
{ temp = l.get(i); l.set(i,l.get(j)); l.set(j,temp) ;
}
}
}
for (int i = 0; i < l.size(); i++) { System.out.println(l.get(i)); } } } |
How to sort and reverse an array list without using sort method
in
arrayList,
Coding Problems,
Java,
java program,
Reverse of array list,
sorting of ArrayList,
Tutorial
- on 01:59:00
- No comments
package test;
import java.util.ArrayList;
import java.util.List;
public class testing {
public static void main(String[] args)
{ List<Integer> l= new ArrayList<Integer>();
l.add(1);
l.add(7); l.add(90); l.add(67); int temp; for (int i = 0; i < l.size(); i++) { for (int j =0 ; j < l.size(); j++) {
if(l.get(i)>l.get(j))
{ temp = l.get(i); l.set(i,l.get(j)); l.set(j,temp) ;
}
}
}
for (int i = 0; i < l.size(); i++) { System.out.println(l.get(i)); } } } |