sweet1cris

Untitled

Feb 9th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1.  
  2. public class Solution {
  3.     /*
  4.      * @param : a continuous stream of numbers
  5.      * @param : a number
  6.      * @return: returns the first unique number
  7.      */
  8.     public int firstUniqueNumber(int[] nums, int number) {
  9.         // Write your code here
  10.         if (nums == null || nums.length == 0) {
  11.             return -1;
  12.         }
  13.        
  14.         Deque<Integer> queue = new LinkedList<>();
  15.         Set<Integer> hash = new HashSet<>();
  16.        
  17.         for (int n : nums) {
  18.             if (hash.contains(n)) {
  19.                 // int index = queue.indexOf(n);
  20.                 // queue.remove(index);
  21.                 queue.removeFirstOccurrence(n);
  22.             } else {
  23.                 hash.add(n);
  24.                 queue.offer(n);
  25.             }
  26.             if (n == number) {
  27.                 return queue.peek();
  28.             }
  29.         }
  30.        
  31.         return -1;
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment