import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TimestampDeduplicator {
/**
* Removes duplicate timestamps from a list of timestamps.
*
* @param timestamps A list of timestamps (represented as long).
* @return A new list containing only unique timestamps, preserving the original order.
*/
public static List<Long> removeDuplicateTimestamps(List<Long> timestamps) {
List<Long> uniqueTimestamps = new ArrayList<>(); // Store unique timestamps
Set<Long> seenTimestamps = new HashSet<>(); // Keep track of seen timestamps
for (Long timestamp : timestamps) {
if (!seenTimestamps.contains(timestamp)) {
uniqueTimestamps.add(timestamp); // Add to unique list if not seen before
seenTimestamps.add(timestamp); // Mark as seen
}
}
return uniqueTimestamps;
}
public static void main(String[] args) {
// Example Usage
List<Long> timestamps = new ArrayList<>();
timestamps.add(1678886400000L);
timestamps.add(1678886400000L);
timestamps.add(1678972800000L);
timestamps.add(1678972800000L);
timestamps.add(1679059200000L);
List<Long> uniqueTimestamps = removeDuplicateTimestamps(timestamps);
System.out.println("Original timestamps: " + timestamps);
System.out.println("Unique timestamps: " + uniqueTimestamps);
}
}
Add your comment