code: def _flip_sub_unique(x: Tensor, k: int) -> Tensor:
      """Get the first k unique elements of a single-dimensional tensor, traversing the
      tensor from the back.
  
      Args:
          x: A single-dimensional tensor
          k: the number of elements to return
  
      Returns:
          A tensor with min(k, |x|) elements.
  
      Example:
          >>> x = torch.tensor([1, 6, 4, 3, 6, 3])
          >>> y = _flip_sub_unique(x, 3)  # tensor([3, 6, 4])
          >>> y = _flip_sub_unique(x, 4)  # tensor([3, 6, 4, 1])
          >>> y = _flip_sub_unique(x, 10)  # tensor([3, 6, 4, 1])
  
      NOTE: This should really be done in C++ to speed up the loop. Also, we would like
      to make this work for arbitrary batch shapes, I'm sure this can be sped up.
      """
      n = len(x)
      i = 0
      out = set()
      idcs = torch.empty(k, dtype=torch.long)
      for j, xi in enumerate(x.flip(0).tolist()):
          if xi not in out:
              out.add(xi)
              idcs[i] = n - 1 - j
              i += 1
          if len(out) >= k:
              break
      return x[idcs[: len(out)]]
  
file_path: botorch/botorch/generation/utils.py
imports: from __future__ import annotations
  import warnings
  from dataclasses import dataclass
  from typing import Callable, Dict, List, Optional, Tuple, Union
  import torch
  from botorch.acquisition import AcquisitionFunction, FixedFeatureAcquisitionFunction
  from botorch.optim.parameter_constraints import _generate_unfixed_lin_constraints, _generate_unfixed_nonlin_constraints
  from torch import Tensor
label_desc: Use image node_id 2 for calling a global function with example usage: _flip_sub_unique(x, k) and returns: x