I am trying to write a program that prints out numbers with unique absolute value. Here is my attempt:
import java.util.*;
public class MyClass {
static ArrayList<Integer> aCopy;
public static void main(String[] args)
{
int myArray[]= {-5, 4,-6,8,-4,6,13};
System.out.println(Arrays.toString(myArray));
aCopy = new ArrayList<>();
for(int i=0; i<myArray.length; i++)
{ aCopy.add(myArray[i]); }
System.out.println("Numbers with unique abs. value are:");
findDifferentAbsoluteValues(myArray);
System.out.println(aCopy);
}
public static boolean findDifferentAbsoluteValues (int[] anArray)
{
for (int i=0; i<anArray.length;i++)
{
for(int j=i+1;j<anArray.length; j++)
{
if ( Math.abs(anArray[i]) == Math.abs(anArray[j]) )
{
aCopy.remove(anArray[i]);
return false;
}
}
}
return true;
}
}
but it gives incorrect output. I am not (yet) very fluent in java so it is not main concern for me than the solution is elegant, just need it working)) Anyone could shed some light what's wrong, please?