Arrays.sort(all);
System.out.println(Arrays.toString(all));
HashSet
for(Integer a:all)
{
set.add(a);
}
System.out.println(set);
int[] input = {2,3,6,6,8,9,10,10,10,12,12};
ArrayList
for(int i = 0 ; i < input.length; i++)
{
if(!list.contains(input[i]))
{
System.out.println(input[i]);
list.add(input[i]);
}
}
System.out.println(list);
private void removeTheDuplicates(List
for(ListIterator
Customer customer = iterator.next();
if(Collections.frequency(myList, customer) > 1) {
iterator.remove();
}
}
System.out.println(myList.toString());
}
Set
If you need to preserve elements order then use LinkedHashSet instead of HashSet
Set
list = new ArrayList
import java.util.ArrayList;
public class Arrays extends ArrayList{
@Override
public boolean add(Object e) {
if(!contains(e)){
return super.add(e);
}else{
return false;
}
}
public static void main(String[] args) {
Arrays element=new Arrays();
element.add(1);
element.add(2);
element.add(2);
element.add(3);
System.out.println(element);
}
}
No comments:
Post a Comment