Gets an array of all the repeated elements

  • Given an array, the array is determined whether it contains. If the output of all the array elements in duplicate, returns true; if not, returns false.
  • Example, as an array of Numbers, the output [2,3,5] Returns true
    int[] numbers = {1,3,4,5,5,3,2,3,2,2,2};

1. Ideas

  Array element numbers in accordance with the two now ascending sort (also descending), followed by comparison of adjacent, if the same two elements, the element was repeated, which was stored in the array duplication.

  Note that, if there are a plurality (more than 3) the same neighbors, repeating array elements Duplication also occurs, if the same four adjacent elements 2 {2,2,2,2}, According to previous practice, duplication array will have three 2 (because three consecutive comparison results are the same), the title does not meet the demand.

  There are ways around this bug two (currently bloggers can only think of two):

1) The continuous number comparison, determines whether or not the current repetitive elements should duplication array. Ideas are as follows:

Comparative int count = 0;

for (....... after traversing sorted array numbers .....) {

  if (a == current array element at the array element)

    temp++;

    if (1 == if the number of comparisons)

      Current repeating elements into an array of duplication;

  else

    = 0 the number of comparisons;

}

2) Set using the set of characteristics - the elements unique. A brief idea is as follows

  Set defines a set of objects, while traversing through the array numbers sorted, added in the set is satisfied if (next == current array element array element && set does not contain the current array element) element,

2. The reference code for

 1 import java.util.*;
 2 
 3 public class TEST{
 4     public static void main(String[] args) {
 5         TEST t = new TEST();
 6         int[] numbers = {1,3,4,5,5,3,2,3,2,2,2};
 7         int[] dup = new int[numbers.length/2+1];
 8 
 9         System.out.println("原numbers数组:");
10         for (int i = 0; i < numbers.length; i++) {
11             System.out.println("numbers["+i+"]:"+numbers[i]);
12         }
13 
14         if(t.duplicate(numbers,numbers.length,dup))
15         {
16             System.out.println("含有重复元素dup:");
17             for (int i = 0; i < dup.length; i++) {
18                 System.out.println("dup["+i+"]:"+dup[i]);
19             }
20         }
21 
22     }
23     public boolean duplicate(int[] numbers,int length,int [] Duplication) {
 24          IF (Numbers == null || length == 0 )
 25              return  to false ;
 26 is  
27          Arrays.sort (Numbers);
 28  
29          int TEMP = 0, J = 0, duplen = 0 ;
 30          for ( int I = 0; I <-length. 1; I ++ ) {
 31 is              iF (Numbers [I] == Numbers [I +. 1 ]) {
 32                  TEMP ++ ;
 33 is                  / * 
34 is                  * if and only if the two adjacent values same time (temp == 1) will accumulate duplicate array duplication to go,
 35                 * If there is a series of three or more adjacent values as the same total of four times 2 {2,2,2,2} comparison,
 36                  * {2,2} comparing the first time, temp ++ == 1, can be stored in duplication array, {2,2,2} and two comparisons ++ == 2 TEMP, TEMP. 3 ++ ==
 37 [                  * TEMP value does not satisfy the condition if (temp == 1), it will not be duplication previously saved data
 38                  * * / 
39                  IF (TEMP ==. 1 ) {
 40                      Duplication [J ++] = Numbers [I];
 41 is                      duplen ++ ;
 42 is                  }
 43 is              } the else {
 44 is                  TEMP = 0 ;}
 45          }
 46 is          IF(duplen == 0){
47             return false;
48         }else {
49             return true;
50         }
51     }
52 }

 

Guess you like

Origin www.cnblogs.com/EricGao/p/11735923.html