Advertisement
nikeza

7. Find the smallest element

Jan 13th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         List<Integer> num = Arrays.stream(scanner.nextLine().split("\\s+"))
  9.                 .map(Integer::parseInt)
  10.                 .collect(Collectors.toList());
  11.  
  12.         int n = num.stream().min((f, s) -> f.compareTo(s))
  13.                 .get();
  14.     long count = num.stream().filter(f->f==n).count();
  15.  
  16.             for (int i = 0; i < num.size(); i++) {
  17.                 if (num.get(i) == n) {
  18.                     if (count==1){
  19.                         System.out.println(i);
  20.                         break;
  21.                     }else {
  22.                         count--;
  23.                     }
  24.  
  25.                 }
  26.             }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement