From CSDN:

# 1. Background

According to the different business logic, we often encounter the situation that there are many duplicate elements in the array. We summarize the sorting of the array and leave a record.

# 2. Implementation method

Four methods are summarized and shown next

## 1. Method 1

```              //Array de duplication method 1
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> result = new ArrayList<>();
boolean flag;
for(int i=0;i<array.length;i++){
flag = false;
for(int j=0;j<result.size();j++){
if(array[i].equals(result.get(j))){
flag = true;
break;
}
}
if(!flag){
result.add(array[i]);
}
}
String[] arrayResult = (String[]) result.toArray(new String[result.size()]);
System.out.println(Arrays.toString(arrayResult));  ```
First traverse the original array, and then traverse the end set. Compare the elements of each array with those of the result set. If they are the same, break. If they are not the same, the result set is saved.
The final result is obtained by traversing two layers of loops.

## 2. Method 2

```//Array de duplication method 2
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> list = new ArrayList<>();
list.add(array[0]);
for(int i=1;i<array.length;i++){
if(list.toString().indexOf(array[i]) == -1){
list.add(array[i]);
}
}
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrayResult));
```

The indexOf method is used to determine whether there are array elements in the result set.

## 3. Method 3

```//Array de duplication method 3
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> list = new ArrayList<>();
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(array[i] == array[j]){
j = ++i;
}
}
list.add(array[i]);
}
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrayResult));  ```

Nested loop, compare to get the result set that meets the conditions.

## 4. Method 4

``` //Array de duplication method 4
String[] array = {"a","b","c","c","d","e","e","e","a"};
Arrays.sort(array);
List<String> list = new ArrayList<>();
list.add(array[0]);
for(int i=1;i<array.length;i++){
if(!array[i].equals(list.get(list.size()-1))){
list.add(array[i]);
}
}
<pre name="code" class="java"><span style="white-space:pre">        </span>String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
```

System.out.println(Arrays.toString(arrayResult));

First, use the array sorting method provided by java to sort, and then carry out a layer of for loop to compare the adjacent data to get the final result set.

## 5. Method 5

```//Array de duplication method 5
String[] array = {"a","b","c","c","d","e","e","e","a"};
Set<String> set = new HashSet<>();
for(int i=0;i<array.length;i++){
set.add(array[i]);
}
String[] arrayResult = (String[]) set.toArray(new String[set.size()]);
System.out.println(Arrays.toString(arrayResult));
```
`String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  `
`System.out.println(Arrays.toString(arrayResult));  `

Original address: http://blog.csdn.net/wenxuechaozh/article/details/52083936

Tags: Java

Posted on Tue, 05 May 2020 05:59:21 -0400 by 2RIP