LeetCode 2882: Drop Duplicate Rows
Problem Description
Explanation
To solve this problem, we can use a HashSet to keep track of the unique emails encountered so far. We iterate through the DataFrame and check if the current row's email is already in the HashSet. If it is not present, we add the email to the HashSet and keep that row in the final result. If the email is already in the HashSet, we skip that row as it is a duplicate.
- Time complexity: O(n) where n is the number of rows in the DataFrame
- Space complexity: O(n) for the HashSet
Solutions
import java.util.HashSet;
import java.util.List;
class Solution {
public List<List<Object>> dropDuplicateRows(List<List<Object>> customers) {
HashSet<String> uniqueEmails = new HashSet<>();
List<List<Object>> result = new ArrayList<>();
for (List<Object> row : customers) {
String email = (String) row.get(2);
if (!uniqueEmails.contains(email)) {
uniqueEmails.add(email);
result.add(row);
}
}
return result;
}
}
Loading editor...