You are given an integer array nums and an integer k. Consider every contiguous subarray (window) of nums of length exactly k.
A value x is a rare-window integer if
x appears in exactly one of those windows, and
within that window, x occurs exactly once.
Return the largest rare-window integer, or -1 if there is none.
Examples
Example 1
Input: nums = [4, 2, 9, 2, 7, 8], k = 3
Output: 8
Explanation: The windows are [4,2,9], [2,9,2], [9,2,7], [2,7,8]. 8 appears only in the last window, once. 9 appears in three windows and 7 in two, so they do not qualify; 4 also qualifies but is smaller.
Example 2
Input: nums = [5, 1, 5, 3, 7], k = 2
Output: 7
Explanation: Windows: [5,1], [1,5], [5,3], [3,7]. 7 appears only in [3,7]; 5, 1 and 3 each appear in two windows.
Example 3
Input: nums = [6, 6, 4, 3], k = 2
Output: 3
Explanation: Windows: [6,6], [6,4], [4,3]. 6 occurs twice in the first window and 4 sits in two windows; 3 appears once in a single window.
Constraints
1≤nums.length≤100
1≤k≤nums.length
0≤nums[i]≤105
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 3
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run