Removing Duplicate Values from ArrayList

后端 未结 18 1278
无人及你
无人及你 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 03:53

    Simple function for removing duplicates from list

    private void removeDuplicates(List list)
    {
        int count = list.size();
    
        for (int i = 0; i < count; i++) 
        {
            for (int j = i + 1; j < count; j++) 
            {
                if (list.get(i).equals(list.get(j)))
                {
                    list.remove(j--);
                    count--;
                }
            }
        }
    }
    

    Example:
    Input: [1, 2, 2, 3, 1, 3, 3, 2, 3, 1, 2, 3, 3, 4, 4, 4, 1]
    Output: [1, 2, 3, 4]

提交回复
热议问题