Can you help me with the method to make the array eliminate the repeated numbers? [closed]

-3
 public static double[] exercici2(double[] a) {
     for (int i = 0; i < a.length; i++) {
        double num = a[i];
        for (int j = i+1; j < a.length; j++) {
            if (num != a[j]) {
               a[i] =a[j];
            }
        }
     }

    return a;
}

public static void main(String[] args)   {

    double[] arraynigga = {3, -5, 2.4, 0, -5, 17, 3};

    double[] a = exercici2(arraynigga);
    System.out.println("{3, -5, 2.4, 0, -5, 17, 3}  "+"  -----> "+arraynigga);

}
    
asked by easymoneysniper 11.04.2018 в 17:46
source

1 answer

-2

See if this solution works for you, it works for me:

public class Test
{
     public static double[] exercici2(double[] a)
     {
        Vector<Double> tmp = new Vector<>();
        for(int i = 0; i < a.length; i++)
        {
            boolean found = false;
            for(int j = 0; j < tmp.size(); j++)
            {
                if(tmp.get(j).doubleValue() == a[i])
                    found = true;
            }
            if(!found)
                tmp.add(a[i]);
        }
        double ret[] = new double[tmp.size()];
        for(int i = 0; i < tmp.size(); i++)
        {
            ret[i] = tmp.get(i);
        }
        return ret;
    }

    public static void main(String[] args)
    {
        double[] arraynigga = {3, -5, 2.4, 0, -5, 17, 3};

        double[] a = exercici2(arraynigga);
        System.out.print("{3, -5, 2.4, 0, -5, 17, 3}    ----->    ");

        System.out.print("{");
        for(int i = 0; i < a.length; i++)
        {
            if(i != 0)
                System.out.print(", ");
            System.out.print(a[i]);
        }
        System.out.print("}");
    }
}
    
answered by 11.04.2018 / 18:15
source