delete array repeated elements

0

My intention is as follows = The user passes us an array of ints (called enters) and we, using a method, have to return an array with the values he has entered, with those that are repeatedly deleted. Here is my code:

public int [] norepeticions (int [] enters) {

    int contador=0;//longitud del nuevo array

    int[] unics=null;//array donde estaran los numeros sin repetir

// In this loop we define the length of the new array (where the numbers go without repeating

    for (int i = 0; i < enters.length-1; i++) {
        if(enters[i]==enters[i+1]){
            contador++;
        }

    }
    unics=new int[contador];

// in this loop we fill the array with the numbers without repeating

    for (int i = 0; i < enters.length-1; i++) {
        if(enters[i]==enters[i+1]){
            unics[i]=enters[i];
        }

    }
    return unics;
}

and the output is as follows:

Array content entered by user: [1 2 3 2] array without repetitions: []

My question is: what have I failed to do? Am I wrong? Is there any other way to do it?

    
asked by david sierra fernandez 03.06.2018 в 18:24
source

2 answers

1

I leave you a class to filter repeated strings in an array :

/** 
 * @file: Strings.php
 * 
 * @utor: Moisés Alcocer, 2016
 * @website: https://www.ironwoods.es
 * @license: http://www.apache.org/licenses/ - Apache License 2.0
 */

package app.helpers;

//imports

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



/** 
 * Class for replace string content according to a rules in an array
 * 
 */
public class Strings {

    /**********************************/
    /*** Properties declaration *******/

        private static List<String> res = new ArrayList<>();


    /**********************************/
    /*** Properties declaration *******/

        /**
         * Cleans the array of empty strings
         *
         * @param     arr   The arr
         * @return   The array
         */
        public static String[] delEmpties( String[] arr ) {

            res.clear();
            int len = arr.length;

            for ( int i = 0 ; i < len; i ++ ) {

                if ( ! arr[ i ].equals( "" ))
                    res.add( arr[ i ] );
            }


            return res.toArray( new String[ 0 ] );
        }

        /**
         * Filters the array of strings and delete repeat elements
         *
         * @param     arr   The arr
         * @return   The array
         */
        public static String[] filterRepeated( String[] arr ) {


            int len = arr.length;

            for ( int i = 0 ; i < len; i ++ ) {
                for ( int j = 0; j < ( len - 1 ); j ++ ) {

                    if ( i != j ) {
                        if ( arr[ i ].equals( arr[ j ] )) {
                            arr[ j ] = ""; //replace value by empty string
                        } 
                    }
                }
            }


            return delEmpties( arr );
        }

} //class

You should be able to adapt the code without too much trouble to other types of elements.

Greetings.

    
answered by 03.06.2018 в 18:37
0

If the user entered the array [1,2,3,2], by traversing the array using the for

for (int i = 0; i < enters.length-1; i++) {
    if(enters[i]==enters[i+1]){
        contador++;
    }

}
unics=new int[contador]; 

is only comparing the current element with the next element, if the repeated numbers are not consecutive they will not count, as in the example. You have to compare each element with all the elements of the arrangement, not only with the next one and also take the account that when comparing it with all the elements of the arrangement in a point will be compared with itself and obviate this situation.

    
answered by 03.06.2018 в 18:39