educative.io

Educative

Feedback on Hashmap Based Solution?

def search_triplets(arr):
  triplets = set()
  num_map = set(arr)

  for left_idx, l_num in enumerate(arr):
    for right_idx, r_num in enumerate(arr[left_idx+1:]):
      target = 0 - l_num - r_num
      if target in num_map and target != l_num and target != r_num:
        triplets.add(tuple(sorted([l_num, target, r_num])))

  return [list(e) for e in triplets]

Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/5679549973004288

Hi @Akshay_Goel
Great job👍🏻, Thank you for sharing it with us.