Removing Duplicate Values from ArrayList

后端 未结 18 1168
无人及你
无人及你 2020-11-30 03:24

I have one Arraylist of String and I have added Some Duplicate Value in that. and i just wanna remove that Duplicate value So how to remove it.

Here Example I got o

相关标签:
18条回答
  • 2020-11-30 04:02

    You can create a LinkedHashSet from the list. The LinkedHashSet will contain each element only once, and in the same order as the List. Then create a new List from this LinkedHashSet. So effectively, it's a one-liner:

    list = new ArrayList<String>(new LinkedHashSet<String>(list))
    

    Any approach that involves List#contains or List#remove will probably decrease the asymptotic running time from O(n) (as in the above example) to O(n^2).


    EDIT For the requirement mentioned in the comment: If you want to remove duplicate elements, but consider the Strings as equal ignoring the case, then you could do something like this:

    Set<String> toRetain = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    toRetain.addAll(list);
    Set<String> set = new LinkedHashSet<String>(list);
    set.retainAll(new LinkedHashSet<String>(toRetain));
    list = new ArrayList<String>(set);
    

    It will have a running time of O(n*logn), which is still better than many other options. Note that this looks a little bit more complicated than it might have to be: I assumed that the order of the elements in the list may not be changed. If the order of the elements in the list does not matter, you can simply do

    Set<String> set = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    set.addAll(list);
    list = new ArrayList<String>(set);
    
    0 讨论(0)
  • 2020-11-30 04:03
         public List<Contact> removeDuplicates(List<Contact> list) {
        // Set set1 = new LinkedHashSet(list);
        Set set = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                     if(((Contact)o1).getId().equalsIgnoreCase(((Contact)2).getId()) ) {
                    return 0;
                }
                return 1;
            }
        });
        set.addAll(list);
        final List newList = new ArrayList(set);
        return newList;
    }
    
    0 讨论(0)
  • 2020-11-30 04:04

    I don't think the list = new ArrayList<String>(new LinkedHashSet<String>(list)) is not the best way , since we are using the LinkedHashset(We could use directly LinkedHashset instead of ArrayList),

    Solution:

    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);
    }
    }
    

    Output: [1, 2, 3]

    Here I am extending the ArrayList , as I am using the it with some changes by overriding the add method.

    0 讨论(0)
  • 2020-11-30 04:04

    Without a loop, No! Since ArrayList is indexed by order rather than by key, you can not found the target element without iterate the whole list.

    A good practice of programming is to choose proper data structure to suit your scenario. So if Set suits your scenario the most, the discussion of implementing it with List and trying to find the fastest way of using an improper data structure makes no sense.

    0 讨论(0)
  • 2020-11-30 04:10
    List<String> list = new ArrayList<String>();
            list.add("Krishna");
            list.add("Krishna");
            list.add("Kishan");
            list.add("Krishn");
            list.add("Aryan");
            list.add("Harm");
    
    HashSet<String> hs=new HashSet<>(list);
    
    System.out.println("=========With Duplicate Element========");
    System.out.println(list);
    System.out.println("=========Removed Duplicate Element========");
    System.out.println(hs);
    
    0 讨论(0)
  • 2020-11-30 04:10
    private static void removeDuplicates(List<Integer> list)
    {
        Collections.sort(list);
        int count = list.size();
        for (int i = 0; i < count; i++) 
        {
            if(i+1<count && list.get(i)==list.get(i+1)){
                list.remove(i);
                i--;
                count--;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题