Repeated elements in an array

3

I'm trying to make an algorithm that finds repeating elements in any array but I do not know how. The algorithm should not check the repetition only in rows or columns, but in the whole array (this seems to me to be the most complicated).

Could anyone give a hint how to implement this?

    
asked by anonymous 20.05.2015 / 15:19

2 answers

1

Solution given in similar question on another network site (SOen) Finding repeats in a 2D array

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[][] ia = new int[3][3];
        ia[0][0] = 1;
        ia[0][2] = 1;
        ia[0][2] = 1;
        ia[1][0] = 4;
        ia[1][3] = 4;
        ia[1][2] = 2;
        ia[2][0] = 5;
        ia[2][4] = 6;
        ia[2][2] = 7;
        findRepeats(ia, ia.length*ia[0].length);
    }

    public static void findRepeats(int [][] num, int size)
{
    int findNum;
    int total = 1, row = 0, col = 0;
    int [] check = new int[size];
    while(row < num.length && col < num[0].length)
    {
        //Set to number
        findNum = num[row][col];
      //Cycle array to set next number
        if(col < num[0].length-1)
            col++;
        else
        {
            row++;      //Go to next row if no more columns
            col = 0;    //Reset column number
        }
        //Loop through whole array to find repeats
        for(int i = row; i < num.length; i++)
        {
            for(int j = col; j < num[i].length; j++)
            {
                if(num[i][j] == findNum) {
                    total++;
                     //Cycle array to set next number
                      if(col < num[0].length-1)
                          col++;
                      else
                      {
                           row++;      //Go to next row if no more columns
                           col = 0;    //Reset column number
                      }
                      if(row < num.length - 1 && col < num[0].length -1)
                         num[i][j] = num[row][col];
                }
            }
        }


        //Display total repeats
        System.out.println("Number " + findNum + " appears " + total + " times.");
        total = 1;
    }
}
}

Running on ideone

    
20.05.2015 / 15:28
1

To walk through an array you need a for within a for , so a possible solution is to go through the array and compare each element by traversing the array. So you will have 4% of nested% s, the two outermost ones are traversing the array for the first time, and the two innermost are picking up the outer iteration element of the array and comparing element to element of the internal iteration. p>

To increase the performance of your comparison, the two most internal% s do not have to start from the for element, the iteration can start from the current element of the outermost iteration. When finding a repeated element you store this value in a for and continue the outermost iteration.

So:

import java.util.ArrayList;
import java.util.List;

public class Matriz {
    public static void main(String[] args) {
        int[][] matriz = new int[][]{
                {1,2,3,4},
                {3,4,5,6},
                {6,7,8,9}};
        List<Integer> repetidos = new ArrayList<Integer>();

        //percorre a matriz, elemento por elemento
        for(int i=0; i<matriz.length; i++) {
            proximoElemento:
            for(int j=0; j<matriz[i].length; j++) {
                //caso elemento já foi marcado como repetido
                //continua para a próxima iteração
                if(repetidos.contains(matriz[i][j])) continue proximoElemento;

                //percorre novamente a matriz, elemento por elemento
                //começando do elemento atual da iteração mais externa 
                for(int i2=i; i2<matriz.length; i2++) {
                    for(int j2=0; j2<matriz[i2].length; j2++) {
                        //não se compara com ele mesmo
                        if(i==i2 && j==j2) break;
                        //achamos um repetido, armazena e 
                        //continua para a próxima iteração 
                        if(matriz[i][j] == matriz[i2][j2]) {
                            repetidos.add(matriz[i][j]);
                            continue proximoElemento;
                        }
                    }
                }
            }
        }

        //exibe os elementos encontrados repetidos ao menos uma vez
        for(int r: repetidos) {
            System.out.println(r);
        }
    }
}

Result:

  

3
  4
  6

Example on Ideone

    
20.05.2015 / 16:52