Input: arr =[2,2,4,4,3,5,3,4,4,6,4,3,3,8], k =4Output: [3,4]Explanation: n =14, n/k =3. Elements 3 and 4 appear 5 and 4 times respectively, which is more than 3.
classSolution {
public: vector<int> findElements(vector<int>& arr, int k) {
int n = arr.size();
vector<int> ans;
for (int i =0; i < n; ++i) {
int cnt =0;
for (int j =0; j < n; ++j) if (arr[j] == arr[i]) ++cnt;
if (cnt > n / k && find(ans.begin(), ans.end(), arr[i]) == ans.end())
ans.push_back(arr[i]);
}
return ans;
}
};
import java.util.*;
classSolution {
public List<Integer>findElements(int[] arr, int k) {
int n = arr.length;
List<Integer> ans =new ArrayList<>();
for (int i = 0; i < n; ++i) {
int cnt = 0;
for (int j = 0; j < n; ++j) if (arr[j]== arr[i]) ++cnt;
if (cnt > n / k &&!ans.contains(arr[i]))
ans.add(arr[i]);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
classSolution:
deffindElements(self, arr: list[int], k: int) -> list[int]:
n = len(arr)
ans: list[int] = []
for i in range(n):
cnt = sum(1for x in arr if x == arr[i])
if cnt > n // k and arr[i] notin ans:
ans.append(arr[i])
return ans
We can have at most k-1 elements that appear more than n/k times. Track up to k-1 candidates and their counts, and reduce counts when the candidate list is full.
#include<unordered_map>classSolution {
public: vector<int> findElements(vector<int>& arr, int k) {
int n = arr.size();
unordered_map<int, int> counter;
for (int x : arr) {
if (counter.count(x)) counter[x]++;
elseif (counter.size() < k-1) counter[x] =1;
elsefor (auto it = counter.begin(); it != counter.end(); )
if (--(it->second) ==0) it = counter.erase(it); else++it;
}
unordered_map<int, int> actual;
for (int x : arr) if (counter.count(x)) actual[x]++;
vector<int> ans;
for (auto& [num, _] : counter)
if (actual[num] > n / k) ans.push_back(num);
return ans;
}
};
import java.util.*;
classSolution {
public List<Integer>findElements(int[] arr, int k) {
int n = arr.length;
Map<Integer, Integer> counter =new HashMap<>();
for (int x : arr) {
if (counter.containsKey(x)) counter.put(x, counter.get(x) + 1);
elseif (counter.size() < k-1) counter.put(x, 1);
else {
Iterator<Map.Entry<Integer, Integer>> it = counter.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
int c = entry.getValue() - 1;
if (c == 0) it.remove();
else entry.setValue(c);
}
}
}
Map<Integer, Integer> actual =new HashMap<>();
for (int x : arr) if (counter.containsKey(x)) actual.put(x, actual.getOrDefault(x, 0) + 1);
List<Integer> ans =new ArrayList<>();
for (int num : counter.keySet())
if (actual.get(num) > n / k) ans.add(num);
return ans;
}
}
classSolution:
deffindElements(self, arr: list[int], k: int) -> list[int]:
n = len(arr)
counter: dict[int, int] = {}
for x in arr:
if x in counter:
counter[x] +=1elif len(counter) < k-1:
counter[x] =1else:
for key in list(counter.keys()):
counter[key] -=1if counter[key] ==0:
del counter[key]
actual: dict[int, int] = {}
for x in arr:
if x in counter:
actual[x] = actual.get(x, 0) +1 ans: list[int] = []
for num in counter:
if actual.get(num, 0) > n // k:
ans.append(num)
return ans