Removing duplicate entries in an array - Java

For Java practice, I am trying to create a method inside my EmployeesDirectory class that:

  • Remove duplicate entries from an array
  • After removing duplicates, the array should have the same length.
  • Non-empty records must make a continuous sequence at the beginning of the array - and actualNum must record records

Duplicate funds: Same name, position and salary

Here is my current code:

I'm not sure how to implement this - any help would be appreciated

class EmployeeDirectory { private Employee dir[]; private int size; private int actualNum; public EmployeeDirectory(int n) { this.size = n; dir = new Employee[size]; } public boolean add(String name, String position, double salary) { if (dir[size-1] != null) { dir[actualNum] = new Employee(name, position, salary); actualNum++; return true; } else { return false; } } } 
+5
source share
4 answers

I would prefer you not to write a great way to remove duplicates. If I were you, I would look for duplicates in the add method, and then immediately decide whether to add Employee .

Also, why don't you use Sets (link for HashSet ) instead of arrays for your purpose? By their own definition, prohibits the addition of duplicates, so they seem suitable as a solution

+4
source

First of all, override the equals and hashCode methods in the Employee class, as follows

 @Override public boolean equals(Object other) { if(this == other) return true; if(other == null || (this.getClass() != other.getClass())){ return false; } Employee guest = (Employee) other; return Objects.equals(guest.name, name) && Objects.equals(guest.position, position) && Objects.equals(guest.salary, salary); } @Override public int hashCode() { return Arrays.hashCode(new Object[] { name, position, salary }); } 

Then you can use the Stream API distinct method to remove duplicates

Returns a stream consisting of individual elements (according to Object.equals (Object)) of this stream.

You can do it like this

 Employee e1 = new Employee("John", "developer", 2000); Employee e2 = new Employee("John", "developer", 2000); Employee e3 = new Employee("Fres", "designer", 1500); Employee[] allEmployees = new Employee[100]; allEmployees[0] = e1; allEmployees[1] = e2; allEmployees[2] = e3; allEmployees = Arrays.asList(allEmployees).stream().distinct() .toArray(Employee[]::new); Arrays.asList(allEmployees).forEach(System.out::println); 

Exit: (saving both empty and non-empty entries)

 John developer 2000.0 Fres designer 1500.0 null 
+3
source

Unfortunately, I don't have an Employee class to test my code, but try the following:

 void removeDuplicates() { int length = dir.length; HashSet set = new HashSet(Arrays.asList(dir)); dir = new Employee[length]; Employee[] temp = (Employee[]) set.toArray(); for (int index = 0; index < temp.length; index++) dir[index] = temp[index]; } 

The code should remain the size of the array after removing duplicates. There must be valid employees at the beginning of the array, null at the end. And don't forget to add this to the top of your .java file.

  import java.util.Arrays; import java.util.HashSet; 
+2
source

If your task says "remove duplicates from the array "(i.e. you cannot use an ArrayList or control when adding elements), you can use the following approach:

 public void removeDuplicates() { Set<Employee> d = new HashSet<>(); // here to store distinct items int shift = 0; for (int i = 0; i > dir.length; i++) { if (d.contains(dir[i])) { // duplicate, shift += 1 shift++; } else { // distinct d.add(dir[i]); // copy to `d` set dir[i - shift] = dir[i]; // move item left } } for (int i = d.size(); i < dir.length; i++) dir[i] = null; // fill rest of array with nulls actualNum = d.size(); } 

Here the shift variable stores the number of duplicates found in the array. Each individual element moves to the shift position on the left to make the sequence continuous, preserving the initial order. Then the remaining elements change to zeros.

To make hash-based assemblies work correctly with Employee instances, you also need to override the hashCode() and equals() methods as follows:

 public class Employee { //... @Override public int hashCode() { return Objects.hash(name, position, salary); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null) return false; if (!o.getType().equals(this.getType()) return false; Employee e = (Employee) o; return Objects.equals(e.name, name) && Objects.equals(e.position, position) && Objects.equals(e.salary, salary); // or e.salary == salary, if it primitive type } } 
+1
source

All Articles