List<Integer> list = new ArrayList<>(1,2,1,3,5);
output will be [2,3,5] without 1
List<Integer> list = new ArrayList<>(1,2,1,3,5);
output will be [2,3,5] without 1
May this can help you:
import java.util.ArrayList;
import java.util.Arrays;
public class RemoveDuplicateElement {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,1,3,5));
ArrayList<Integer> duplicateElementsRemovedList = new ArrayList<Integer>();
for (Integer elementToSearch :list) {
int found=0;
for (Integer element :list) {
if(elementToSearch.equals(element)) {
found++;
}
}
if(found==1)
{
duplicateElementsRemovedList.add(elementToSearch);
}
}
for (Integer element :duplicateElementsRemovedList) {
System.out.println(element);
}
}
}
If you want to have only unique records you can use HashSet, however as you mentioned you want to remove all elements which are duplicates, you can use this simplest piece of code to do it, what you will need to do is create a new list which will only contain uniques and remove duplicates..take a look
List cleanList = new ArrayList<Integer>();
for(int n=0;n<list.size();n++){
if(!cleanList.contains(list.get(n))){
cleanList.add(list.get(n));
}else{
cleanList.remove(list.get(n));
}
}
The result in cleanList will be 3, 5. See if you can use this or get ideas from the above script
You could have googled once before you posted your question here, anyway check this link out.
A part of an array cannot be deleted. You have two options, either you can shift elements to the left or create a new array without the duplicate elements and delete the old array.
This might solve your query.