I have got my result based on below approach, but I'm not satisfied.
I want some other way and efficient manner to get the expected output.
Input:
112341
Output:
1234
No duplicates should be displayed in output and answer must be 1234
.
My solution :
public class PrintUnique {
public static void main(String[] args) {
int result = printUniquNums(112341);
System.out.println(result);
}
private static int printUniquNums(int num) {
String nums = Integer.toString(num);
char [] ch = nums.toCharArray();
Set<Character> store = new LinkedHashSet<>();
String res = "";
for (int i = 0; i < ch.length; i ){
if (store.add(ch[i])){
res = ch[i];
}
}
return Integer.parseInt(res);
}
}
CodePudding user response:
A little tweaks with Java lambdas and streams. Your code is also good.
public static void main(String[] args) {
int result = printUniquNums(112341);
System.out.println(result);
}
private static int printUniquNums(int num) {
String nums = Integer.toString(num);
List<String> digits = List.of(nums.split(""));
Set<String> uniqueDigits = new HashSet<>();
String uniqueNum = digits.stream().filter(uniqueDigits::add).reduce(String::concat).orElse(""); // set add method returns true if value not present
return Integer.parseInt(uniqueNum);
}
CodePudding user response:
This is not more effecient in term of performance. But its fewer lines of code and easier to read which in most cases is more important.
private static int printUniquNums(int num) {
TreeSet<String> tree= new TreeSet<>();
for (char c : Integer.toString(num).toCharArray()) {
tree.add(String.valueOf(c));
}
return Integer.parseInt(String.join("", tree));
}
CodePudding user response:
Another approach, which doesn't use strings, implies to keep dividing the number by 10 to power the number's digits minus one and collect each digit from left to right in a Set
, specifically a LinkedHashSet
to maintain the insertion order. Then, using streams to rebuild the number without duplicate digits:
public static int uniqueNumber(int num) {
int temp;
Set<Integer> set = new LinkedHashSet<>();
while (num > 10) {
temp = (int) Math.pow(10, Math.ceil(Math.log10(num)) - 1);
set.add(num / temp);
num %= temp;
}
set.add(num);
Integer[] digits = set.toArray(new Integer[set.size()]);
return IntStream.range(0, digits.length)
.map(i -> digits[i] * ((int) Math.pow(10, (digits.length - 1) - i)))
.reduce(0, (a, b) -> a b);
}
Output
1234
Here is a link to test the code:
https://www.jdoodle.com/iembed/v0/rSi
CodePudding user response:
Your method can be reimplemented using Stream API.
One of the simplest ways to do it is to create a stream of code points.
Then we need to apply distinct()
to insure uniqueness of the stream elements. This operation utilizes a LinkedHashSet
under the hood to reject duplicates and preserve the initial order.
And finally, apply a terminal operation collect()
by making use StringBuilder
as a container for performing mutable reduction.
private static int printUniquNums(int num) {
String result = String.valueOf(num).codePoints() // generating a stream of code points
.distinct() // ensure uniqueness
.collect(StringBuilder::new, // mutable container
StringBuilder::appendCodePoint, // defining how values should be accumulated in the container
StringBuilder::append) // how to merge the containers obtained while executing the stream in parallel
.toString();
return Integer.parseInt(result);
}
Another way of achieving this is to map stream elements into String
type and make use of the built-in collector joning()
that will take care of the stream concatenation for as:
private static int printUniquNums(int num) {
String result = String.valueOf(num).codePoints() // generating a stream of code points
.distinct() // ensure uniqueness
.map(Character::getNumericValue) // getting a numeric value from a code point
.mapToObj(String::valueOf) // creating a single-character string
.collect(Collectors.joining()); // combining the strings together
return Integer.parseInt(result);
}
public static void main(String[] args) {
int result = printUniquNums(112341);
System.out.println(result);
}
Output (for both solutions):
1234