Home > Net >  Find numbers with same first and last digits
Find numbers with same first and last digits

Time:11-04

Sorry if I type something wrong, I am new. I want to create a method that takes an array, and gives back an array with the numbers that have the same first and last digits in the previous array.Example: 12 is equal to 1342.

I have created this for loop to go through the numbers, but I don't know how to compare them.

public int[] findDuplicates(int[] a) {
      List<Integer> result = new ArrayList<>();
        for (int i = 0; i < a.length; i  ) {
            for (int j = i   1; j < a.length; j  ) {
                
                if ((//what do I write here) ) {
                    //and here

                }
                
            }
        }

        return result.stream()
            .mapToInt(Integer::intValue)
            .toArray();
    }

CodePudding user response:

The easiest way is to parse your numbers into strings and compare them. I did it this way :

public int[] findDuplicates(int[] a) {
    List<Integer> result = new ArrayList<>();
    boolean[] numbersThatHaveBeenAdded = new boolean[a.length];

    for (int i = 0; i < a.length; i  ) {
        for (int j = i   1; j < a.length; j  ) {

            String iNumber = String.valueOf(a[i]);
            String jNumber = String.valueOf(a[j]);
            if (iNumber.charAt(0) == jNumber.charAt(0)
                && iNumber.charAt(iNumber.length()-1) == jNumber.charAt(jNumber.length()-1)) {

                if (!numbersThatHaveBeenAdded[i]) {
                    result.add(a[i]);
                    numbersThatHaveBeenAdded[i] = true;
                }
                if (!numbersThatHaveBeenAdded[j]) {
                    result.add(a[j]);
                    numbersThatHaveBeenAdded[j] = true;
                }
            }

        }
    }

    return result.stream()
        .mapToInt(Integer::intValue)
        .toArray();
}

And I used a boolean array to keep in memory the numbers I already added in the result, to avoid duplication.

CodePudding user response:

You can use the while loop (number /= 10) algorithm to get the first digit.
And number % 10 to get the last digit.
And then you can compare between them with if condition, if true you add that number to the list.
And do not forget the return type of that method it must be List

public List<Integer> findDuplicates(int[] array){
    List<Integer> result = new ArrayList<>();
    for (int i = 0; i < array.length; i  ) {
        int firstDigit = array[i];
        int lastDigit = array[i] % 10;
        while (firstDigit >= 10)
            firstDigit /= 10;
        if (firstDigit == lastDigit)
            result.add(array[i]);
    }
    return result;
}

CodePudding user response:

You can do it pretty easily like this using a log function. Here, ^ is exponentiation. This uses the fact that for some number n, n = 10^y, where y = log10(n). Unless y is a power of 10, it will have a fractional part which should be ignored. So log10(12345) = 4.091491094267951. Ignoring the fraction and computing 10^4 == 10,000 which will yield 1 when dividing 12345. The right most digit is simply 12345 % 10 using the remainder operator.

  • The testData is grouped in pairs (e.g. target, digits, target, digits,...)
  • The values are passed to the method and the result printed using the conditional operator
  • The digits are expressed in a two digit value. Leading zeroes of the arguments are not considered. Unless you pass strings, 0123 == 123.
int[] testData = {1,11, 1,22, 1234,14, 142023, 13, 10202,15, 10102,15, 1000, 10};
for  (int i = 0; i < testData.length; i =2) {
    int val = testData[i];
    int digits = testData[i 1];
    System.out.printf("           
  • Related