Home > Enterprise >  How to implement the concept of duodigit in java
How to implement the concept of duodigit in java

Time:10-19

i am trying to solve a problem about duodigit concept in java.

Here it is:

We call duodigit an integer whose decimal representation does not use more than two different digits.

For example :

  • 12 , 110 , -33333 : are all duodigits , since they have no more than two different digits
  • 102 : is not a duodigit since his digits 1 , 0 and 2 are three different digits.

Implement the isDuoDigit(number) method which returns a string:

  • y if number is a duodigit
  • n otherwise

EXAMPLE:

enter image description here

public class Solution {

    public static String isDuoDigit(int number){
        /* Write your code here */

    return "";
    }
}

So i came up with this solution:

public class Solution_duoDigit {

    public static String isDuoDigit(int number) {
        /* Write your code here */
        Set<Integer> set= new HashSet<>();
        set.add(number);
        if(set.size()<=2){
            return "y";
        }else{
            return "n";
        }
        
    }

    public static void main(String[] args) {

        System.out.println(Solution_duoDigit.isDuoDigit(12));
        System.out.println(Solution_duoDigit.isDuoDigit(110));
        System.out.println(Solution_duoDigit.isDuoDigit(102));
        System.out.println(Solution_duoDigit.isDuoDigit(-2021));

    }
}

But my solution is not complete and doesn't work because I do not know how to express the fact that it does not have more than two different digits in java code. Can you help me please?

CodePudding user response:

You can use .chars():

public static String isDuoDigit(int number) {
    boolean isDuoDigit = Integer.toString(number).replace("-", "").chars().distinct().count() <= 2;
    return isDuoDigit ? "y" : "n";
}

Alternatively you can do:

public static String isDuoDigit(int number) {
    boolean isDuoDigit = Integer.toString(Math.abs(number)).chars().distinct().count() <= 2;
    return isDuoDigit ? "y" : "n";
}

Then:

System.out.println(isDuoDigit(12));
System.out.println(isDuoDigit(110));
System.out.println(isDuoDigit(-3333));
System.out.println(isDuoDigit(102));

Output:

y
y
y
n

CodePudding user response:

Well, using a Set is not a bad idea, as it eliminates the duplicates.

The problem with your current code is that you add the whole integer to the set, which results in the set having just a single value.

What you need to do, is adding each digit separately to the set.

You need to do a few things:

  • Remove any sign, as this obviously does not count as digit;
  • Get each digit separately;
  • For each digit, add it to the set;
  • Get the size of the set.
String s = Integer.toString(Math.abs(number));
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i  ) {
    char c = s.charAt(i);
    set.add(c);
}
return (set.size() <= 2 ? "y" : "n");

Note: I could have used Java Streams instead, but I'm not sure if you're accustomed to them yet. Here's a one-liner using streams:

return Integer.toString(Math.abs(number)).chars().distinct().count() <= 2 ? "y" : "n";

But that's indeed pretty much what Oboe already said in his answer.

CodePudding user response:

and also you can use the below,

        int i = -3333;
        String s = String.valueOf(i).replace("-", "");
        Map<Integer, Integer> map = new HashMap<>();
        for (int j = 0; j < s.length(); j  ) {
            map.merge((int) s.charAt(j), 1, Integer::sum);
        }
        long count = map.values()
                        .size();
        System.out.println(count > 2 ? "true" : "false");
  •  Tags:  
  • java
  • Related