Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /*
- * @param : a continuous stream of numbers
- * @param : a number
- * @return: returns the first unique number
- */
- public int firstUniqueNumber(int[] nums, int number) {
- // Write your code here
- if (nums == null || nums.length == 0) {
- return -1;
- }
- Deque<Integer> queue = new LinkedList<>();
- Set<Integer> hash = new HashSet<>();
- for (int n : nums) {
- if (hash.contains(n)) {
- // int index = queue.indexOf(n);
- // queue.remove(index);
- queue.removeFirstOccurrence(n);
- } else {
- hash.add(n);
- queue.offer(n);
- }
- if (n == number) {
- return queue.peek();
- }
- }
- return -1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment