如何删除三重线

时间:2016-06-06 15:29:16

标签: java arrays for-loop

以下是我的信息

63974241
63974241
63974241
68936124
68936124
74513945
74513945
76343943
76343943
76343943
85028969
85028969
91809014
109367944
109367944
109367944

我想删除那些一式三份,但保留单身和重复。 因此,期望输出应如下所示,

68936124
68936124
74513945
74513945
85028969
85028969
91809014

以下是我现在的脚本:

for (int row = 0; row < row_number; row++) {
    if (array[row].equals(array[row+1]) && array[row+1].equals(array[row+2]) ) {                    
    }
    else if (array[row].equals(array[row+1]) && array[row-1].equals(array[row]) ) {
    }
    else if (array[row].equals(array[row-1]) && array[row-1].equals(array[row-2]) ) {
    }
    else{ 
        System.out.println(array[row+1]);
        }               
}

如果有任何帮助,我将不胜感激。

5 个答案:

答案 0 :(得分:1)

对于每个元素检查:

  • 前两个元素
  • 两个后续元素
  • 前一个和后一个元素

如果之前没有任何重复添加到您的结果中。

例如,将星形视为当前元素:

 A*, A, A, B, C    Don't add because two following elements are equals
 A, A*, A, B, C    Don't add because previous and next elements are equlas
 A, A, A*, B, C    Don't add because previous two elements are equals
 A, A, A, B*, C    Add because no condition is true
 A, A, A, B, C*    Add because no condition is true           

以下是相同的代码:

// Assuming rows is ordered so that equals elements are close each other

public List noTriplicates(Object[] rows) {
    List results = new ArrayList();
    for (int i = 0; i < rows.length; i++) {
        boolean add = true;

        // Check for two previous
        if (i >= 2 && rows[i-2].equals(rows[i])) {
           add = false;

        // Check previous and next
        } else if (i >= 1 && i < rows.length - 1
                       && rows[i-1].equals(rows[i]) 
                       && rows[i+1].equals(rows[i])) {
           add = false;

        // Check for two next
        } else if (i < rows.length - 2 && rows[i+2].equals(rows[i])) {
           add = false;
        }
        if (add) {
            results.add(rows[i]);
        }

    }
    // Here results has no triplicates
    return results;
}

请注意,也可以将所有条件与或运算符组合在一起,但代码的可读性会降低。

答案 1 :(得分:0)

您可能希望有3个嵌套循环,但时间应为O(n log ^ 2 n):

    boolean triplicates;
    int[] array = initArray();

    // track triplicates. If you don't, then time is O(n^3), as you need to start over for every nested loop
    boolean[] triplicatesPositions = new boolean[array.length];
    Arrays.fill(triplicatesPositions, false); // not necessary, default is false
    for (int j = 0; j < array.length; j++) {
        if (!triplicatesPositions[j]) {
            triplicates = false;
            // no need to start from the beginning
            for (int k = j + 1; k < array.length; k++) {
                if (array[k] == array[j]) {
                    // no need to start from the beginning
                    for (int i = k + 1; i < array.length; i++) {
                        if (array[i] == array[k]) {
                            triplicates = true; // got it!
                            // remember for the future these are triplicates
                            triplicatesPositions[k] = true; 
                            triplicatesPositions[i] = true;
                        }
                    }
                }
            }
            // this way, you don't print the triplicates at all. If you want to print the first and second time they appear, then simply remove the if statement and print anyway
            if (!triplicates)
        }
    }

不需要订购元素。 适应this

答案 2 :(得分:0)

如果订购了数组,我会这样做:

 String lastTriplet="";   
    for (int row = 0; row < row_number; row++) {
     if ((row>=row_number-2) || ( row+2<row_number && !array[row].equals(array[row+2]))){
       if (!lastTriplet.equals(array[row])) System.out.println(array[row]);
    }else{
        lastTriplet=array[row];
        row=row+2;
      }
    }

答案 3 :(得分:0)

如果排序了数组,那么我们可以修改代码如下:

String row_data = array[0];
int count = 0;
if(array.length == 1) 
    System.out.println(row_data);
else
    for (int row = 1; row < array.length; row++) {
        if(row_data == array[row]) {
            count++;
        } else {
            if(count < 3) {
                print(row_data, count);
            }
            row_data = array[row];
            count = 1;
        }
        if(row+1 == array.length && count < 3) {
            print(row_data, count);
        }
    }

下面是print()方法:

   void print(String row_Data, int count) {
       for(int i=0; i<count; i++) {
           System.out.println(row_Data);
       }
   }

输出:

68936124
68936124
74513945
74513945
85028969
85028969
91809014

答案 4 :(得分:0)

我对溪流不好,但是这样的东西可以用吗?

Map m = IntStream.of(array).boxed().
        collect(Collectors.groupingBy(n -> n, Collectors.counting()));

array = IntStream.of(array).boxed().filter(n -> {
  long v = (long) m.get(n);
  m.put(n, v > 2 ? 1 : v - 1);
  return v > 0;
}).mapToInt(i -> i).toArray();

保留订单(如果找到3个相同的数字,它将删除最后一个)。

如果您有这样的数组:

int[] array = { 1, 1, 1, 2, 3, 2, 3, 2, 3 };

你将拥有:

{ 1, 1, 2, 3, 2, 3 }