删除ArrayList中的重复元素而不使用HashSet

时间:2012-10-02 19:46:50

标签: java algorithm arraylist

我的逻辑搞砸了。我只是试图找到一种方法来删除ArrayList中的重复项而不使用HashSet。

public static void main(String[] args) {
    ArrayList<String> wordDulicate = new ArrayList<String>();
    wordDulicate.add("Tom");
    wordDulicate.add("Jones");
    wordDulicate.add("Sam");
    wordDulicate.add("Jamie");
    wordDulicate.add("Robie");
    wordDulicate.add("Helen");
    wordDulicate.add("Tom");
    wordDulicate.add("Troy");
    wordDulicate.add("Mika");
    wordDulicate.add("Tom");

    for (String dupWord : wordDulicate) {
        if (wordDulicate.equals(dupWord))
            System.out.println(wordDulicate.get(dupWord));
    }
}

8 个答案:

答案 0 :(得分:9)

您可以创建另一个列表,并向其中添加项目,跳过重复项: -

ArrayList<String> wordDulicate = new ArrayList<String>();
ArrayList<String> tempList= new ArrayList<String>();

wordDulicate.add("Tom");
wordDulicate.add("Jones");
wordDulicate.add("Sam");


for (String dupWord : wordDulicate) {
    if (!tempList.contains(dupWord)) {
        tempList.add(dupWord);
    }
}

答案 1 :(得分:5)

此方法需要另一个列表:

       ArrayList<String> wordDulicate = new ArrayList<String>();

        wordDulicate.add("Tom");
        wordDulicate.add("Jones");
        wordDulicate.add("Sam");
        wordDulicate.add("Jamie");
        wordDulicate.add("Robie");
        wordDulicate.add("Helen");
        wordDulicate.add("Tom");
        wordDulicate.add("Troy");
        wordDulicate.add("Mika");
        wordDulicate.add("Tom");

        ArrayList<String> nonDupList = new ArrayList<String>();

        Iterator<String> dupIter = wordDulicate.iterator();
        while(dupIter.hasNext())
        {
        String dupWord = dupIter.next();
        if(nonDupList.contains(dupWord))
        {
            dupIter.remove();
        }else
        {
            nonDupList.add(dupWord);
        }
        }
      System.out.println(nonDupList);

输出:

[Tom, Jones, Sam, Jamie, Robie, Helen, Troy, Mika]

答案 2 :(得分:4)

您应该对列表进行排序,并删除与前一个元素相同的元素。

答案 3 :(得分:2)

似乎很晚才参加聚会,但是只有单一的arraylist,

这里是没有hashset的代码......

strings = new ArrayList<>();
    strings.add("a1");
    strings.add("a2");
    strings.add("ss");
    strings.add("asd");
    strings.add("asd");
    strings.add("a1");
    strings.add("ss");
    strings.add("ss");
    System.out.println(Collections.frequency(strings, "ss"));
    for(int i = 0; i< strings.size();i++){
        int frequency = Collections.frequency(strings, strings.get(i));
        if(frequency>1){
            String string = strings.get(i);
            for(int j = 1 ; j < frequency ; j++)
                strings.remove(string);
        }
    }
    System.out.println(Collections.frequency(strings, "ss"));
    System.out.println(strings);

答案 4 :(得分:1)

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ArrayListDuplicateRemove {

    public static void main(String[] args) {


       List<String> lst = new LinkedList<String>();
        lst.add("ABC");
        lst.add("ABC");
        lst.add("ABCD");
        lst.add("ABCD");
        lst.add("ABCE");

        System.out.println("Duplicates List "+lst);

        Object[] st = lst.toArray();
          for (Object s : st) {
            if (lst.indexOf(s) != lst.lastIndexOf(s)) {
                lst.remove(lst.lastIndexOf(s));
             }
          }

        System.out.println("Distinct List "+lst);

    }

}

答案 5 :(得分:1)

package arraylist;

import java.util.ArrayList;
import java.util.Iterator;

class Student{
    String name;
    String dob;
    public Student(String name, String dob) {
        super();
        this.name = name;
        this.dob = dob;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDob() {
        return dob;
    }
    public void setDob(String dob) {
        this.dob = dob;
    }

    public String toString() {
        return name+"\t"+dob;
    }
}

public class RemoveDuplicate {

    public static void main(String[] args) {

        ArrayList<Student> list=new ArrayList<Student>();
        list.add(new Student("prakash", "89"));
        list.add(new Student("prakash", "89"));
        list.add(new Student("saurabh", "79"));
        list.add(new Student("saurabh", "79"));

        Iterator<Student> it=list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        int size=list.size();
        for(int i=0;i<size-1;i++){
            for(int j=i+1;j<size-1;j++){
                if(list.get(i).getName().equals(list.get(j).getName())){
                    list.remove(j);
                }
            }
        }
            System.out.println(list);
    }
}

答案 6 :(得分:0)

 ArrayList l1 = new ArrayList();
ArrayList l2 = new ArrayList();

Iterator iterator = l1.iterator();

    while (iterator.hasNext())
    {
        YourClass o = (YourClass) iterador.next();
        if(!l2.contains(o)) l2.add(o);
    }

l1是原始列表,l2是没有重复项目的列表(确保您的类具有根据您想要的平等对应的等于方法)

答案 7 :(得分:0)

ArrayList<Integer> al = new ArrayList<Integer>();

Scanner sc = new Scanner(System.in);
for(int i=0;true;i++){
    System.out.println("Enter your Element:");
     String s1= sc.nextLine();
     if(s1.equals("Exit")){
         break;
     }
     int x1= Integer.parseInt(s1);
     al.add(x1);

     for(int j=0;j<al.size();j++){
         for(int k=j+1;k<al.size();k++){

             int temp;
             if(al.get(j).intValue()>al.get(k).intValue()){
                 temp =al.get(j).intValue();
                 al.set(j, al.get(k).intValue());
                 al.set(k, temp);

             }//if
         }//for
     }//for
}//for
System.out.println(al);