Tom
Tom

Reputation: 21

How to find unique number from a user inputted sequence, if there is one

User will input a sequence of numbers, integers separated with commas, like this 2,3,1,245,2,75. How can I find if there is a number that is not duplicated and print it back ? I got so far this

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    String line;
    String[] line2;

    line = reader.nextLine();
    line2= line.split(",");
    int n=0;
    for(int i=0; i<line2.length; i++){
        System.out.println(line2[n]);
        n++;
    }
}

What this does is: stores the input in string "line" and then removes the commas and stores only the numbers in "line2". And then prints the content of line2 but thats not necessary I did it just to see if it works, what i need to print is just the unique number.

Upvotes: 2

Views: 420

Answers (4)

Mureinik
Mureinik

Reputation: 311938

You could stream the array you get from splitting the strings, create a frequency map of it, and keep just the numbers that appear once:

List<String> unique =
        Arrays.stream(input.split(","))
              .collect(Collectors.groupingBy(Function.identity(), 
                                             Collectors.counting()))
              .entrySet()
              .stream()
              .filter(e -> e.getValue() == 1)
              .map(Map.Entry::getKey)
              .collect(Collectors.toList());

Upvotes: 4

fireandfuel
fireandfuel

Reputation: 732

With Java 8's Streams: I first filter the Array of Strings to remove all duplicates, than check if the string is unique and finally print all unique Strings out.

import java.util.Scanner;
import java.util.stream.Stream;

public class UniqueNumberScanner {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String line;
        String[] line2;

        line = reader.nextLine();

        line2 = line.split(",");
        Stream.of(line2).distinct().filter(item -> Stream.of(line2).filter(item::equals).count() == 1).forEach(System.out::println);
    }
}

Upvotes: 0

progyammer
progyammer

Reputation: 1508

Iterate over the array and check whether there are copies for each element.

for(String x:line2){
    if(unique(x, line2))
        System.out.println(x+" is a unique number");
}

Here's the unique() function that checks for copies.

private static boolean unique(String x, String[] line2){
    for(String i:line2){
        if(i.equals(x))
        return false;
    }
    return true;
}

Upvotes: 1

ItamarG3
ItamarG3

Reputation: 4122

You can put the numbers in a List<> and then use Collections.frequency(), which is more efficient than iterating over the collection yourself:

Scanner reader = new Scanner(System.in);
String line;
String[] line2;
line = reader.nextLine();
line2= line.split(",");
List<Integer> list = new ArrayList<>();
for(int i=0; i<line2.length; i++){
    list.add(Integer.parseInt(line2[i]));
}
boolean flag = true;
for(int a : list){
    if(Collections.frequency(list, a)!=1){
        flag = false;
    }
}

NOTE: this might throw NumberFormatException if the user put something which isn't a number

Upvotes: 0

Related Questions