Use HashSet rather than LinkedList to store indices