0

How we can delete duplicate entries from StudentList object. Here we can identify student is duplicate or not if more according to entry sequence.

class Student
{
  String name;
  long phone;
  String address;
  boolean isActive;
  Student(String name, long phone, String address, boolean isActive)
  {
    this.name = name;
    this.phone = phone;
    this.address = address;
    this.isActive = isActive;
  }
}

this is execution class.

class CollegeMgmt
{
  public static void main(String s[])
  {
    ArrayList<Student> studentList = new ArrayList<Student>();
    Student s1 = new Student("devid", 9898989898L, "Stainford d23", true);
    studentList.add(s1);
    Student s2 = new Student("sames", 9895555598L, "Stainford d23", true);
    studentList.add(s2);
    Student s3 = new Student("devid", 9898989898L, "Stainford d23", false);
    studentList.add(s3);
    Student s4 = new Student("devid", 9898989898L, "Stainford d23", false);
    studentList.add(s4);
    Student s5 = new Student("devid", 9898989898L, "Stainford d23", true);
    studentList.add(s5);
  }
}

I want to know which approach will be best to delete all duplicate entries from studentList. duplicate is here if two or more entry are same as

    Student s3 = new Student("devid", 9898989898L, "Stainford d23", false);
    studentList.add(s1);
    Student s4 = new Student("devid", 9898989898L, "Stainford d23", false);
    studentList.add(s1);

this is on the behalf of isActive bit, if in our list continues same bit we are setting then that object is need to delete. there s4 object should be remove no other object should be remove.

Vinay Sharma
  • 1,163
  • 1
  • 10
  • 20
  • over there they are showing removing duplicate items for string objects and that way can not be applied to above type of problems. Using inner and outer loop we can delete. But i want to know that is there any other way? – Vinay Sharma Apr 05 '15 at 18:15
  • @VinaySharma This sounds pretty much like you ask other people to do your homework. Do you really think you learn more from not even trying to solve your assignments yourself? – GhostCat Apr 05 '15 at 18:24
  • No this is not right, i have done it already but i think complexity is to much that is O(2n) so i want to know best solution for such type of problems. – Vinay Sharma Apr 05 '15 at 18:35
  • Jägermeister would you like to say some thing on above problem. I have resolve this problem by using two loops. Are you having other solution for that. – Vinay Sharma Apr 05 '15 at 18:43

2 Answers2

3

You should probably start by overriding Object.equals in Student (by testing the fields that determine equality). Next, use a Set which will prevent duplicates (a LinkedHashSet, for example, preserves insertion order).

candied_orange
  • 7,036
  • 2
  • 28
  • 62
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

Use Set interface to avoid duplicate values. The Set Interface

Example of Duplicate value

Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24