Java implementation of the removal of duplicate data array method details

  • 2020-11-03 22:07:20
  • OfStack

This article illustrates how Java implements an array to remove duplicate data. To share for your reference, the details are as follows:

Some time ago, I was asked: If there are duplicate elements in an array, what method can I use to unduplicate the elements? 1 Time will think of using 1 method, but later after looking up information, found that there are many ways to achieve, now I will summarize 1, the relatively simple several.

1. Implementation with List collection


int[] str = {5, 6, 6, 6, 8, 8, 7,4};
List<Integer> list = new ArrayList<Integer>();
for (int i=0; i<str.length; i++) {
  if(!list.contains(str[i])) {
    list.add(str[i]);
  }
}
System.out.println(" Remove the repeated ones list A collection of "+list);

The output result is:


 Remove the repeated ones list A collection of [5, 6, 8, 7, 4]

You can see that duplicate elements can be removed, but sorting is not implemented.

2. hashSet or TreeSet


Integer[] nums = { 5, 5, 6, 6, 6, 8, 8, 7, 11, 12, 12 };
// HashSet hset = new HashSet(Arrays.asList(nums));
TreeSet<Integer> hset = new TreeSet<Integer>(Arrays.asList(nums));
Iterator i = hset.iterator();
while(i.hasNext()){
  System.out.println(i.next());
}

Output results:


5 6 7 8 11 12

You can see that not only is duplicate data removed, but the data is sorted.

Among them Arrays.asList() This method returns an object of type ArrayList. The ArrayList class is not java.util.ArrayList, but the static inner class of the Arrays class!

TreeSet not only keeps elements from repeating, but also implements collections of functions such as sorting, automatically inserting object elements into an ordered sequence of objects according to some kind of comparison rule when they are added to the collection.

3. List and set


int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
List<Integer> numList = new ArrayList<Integer>();
for (int i : nums)
  numList.add(i);
Set<Integer> numSet = new HashSet<Integer>();
numSet.addAll(numList);
System.out.println(numSet);

Output results:


[5, 6, 7, 8]

As you can see, the duplicate data is also removed, and sorting is implemented.

Let's make a comparison between HashSet and TreeSet:

HashSet

HashSet has the following characteristics

1) The order of elements cannot be guaranteed, and the order may change
2) Not in sync
3) The set element can be null, but only one null can be placed

When storing an element in the HashSet collection, HashSet calls the object's hashCode() method to get the object's hashCode value, and then determines where the object will be stored in HashSet based on the hashCode value.

Simply put, the criteria for the HashSet set to determine the equality of two elements is that two objects are compared to each other by the equals method, and the return value of the hashCode() method of the two objects is equal

Note that if you want to put an object in HashSet, override the equals method of the object's corresponding class and override its hashCode() method. The rule is that if two pairs of images return true by equals method comparison, their hashCode should be the same. In addition, any property in the object that is used as an equals comparison standard should be used to calculate the value of hashCode.

TreeSet class

TreeSet is the only implementation class of the SortedSet interface, and TreeSet ensures that collection elements are sorted. TreeSet supports two sorts, natural sort and custom sort, where natural sort is the default sort. Objects of the same class should be added to TreeSet.

The way TreeSet determines that two objects are not equal is if the two objects return false via the equals method, or if the comparison does not return 0 via the CompareTo method

Natural ordering

Natural sorting USES the CompareTo (Object obj) method of the elements to sort to compare the size relationships between the elements, and then sorts the elements in ascending order.

Java provides an Comparable interface, in which an compareTo(Object obj) method is defined. This method returns an integer value, and objects implementing the interface can be compared in size.

The es117EN1.compareTo (obj2) method returns 0 to indicate that the two objects being compared are equal, a positive number to indicate that obj1 is greater than obj2, and a negative number to indicate that obj1 is less than obj2.

If we always return true for both objects, the compareTo method for both objects should return 0

Custom sorting

Natural sorting is arranged in ascending order according to the size of the collection elements. If you want to customize sorting, you should use the Comparator interface to implement the int compare(T o1,T o2) method.

The most important:

1. TreeSet is implemented by 2-difference tree. The data in Treeset is automatically sorted, and the value of null is not allowed.

2. HashSet is implemented by hash table. The data in HashSet is unordered and can be put into null, but only one can be put into null.

3. HashSet requires that the object put in must implement HashCode() method. The object put in is identified by hashcode code, while the String object with the same content, hashcode is one, so the content put in cannot be repeated. But objects of the same class can be put into different instances.

PS: There are also two simple and practical online text repeating tools on this site, which are recommended for everyone to use:

Online tool for removing duplicates:
http://tools.ofstack.com/code/quchong

Online text de-duplication tools:
http://tools.ofstack.com/aideddesign/txt_quchong

For more information about java, please refer to Java array operation Skills Summary, Java character and string operation Skills Summary, Java Mathematical Operation Skills Summary, Java Data Structure and Algorithm Tutorial and Java Node Operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: