Find common elements In 3 sorted arrays in Python

prepinsta-prime

Get over 200+ Courses under One Subscription

mute

Don’t settle Learn from the Best with PrepInsta Prime Subscription

Learn from Top 1%

One Subscription, For Everything

The new cool way of learning and upskilling -

Limitless Learning

One Subscription access everything

Job Assistance

Get Access to PrepInsta Prime

Top Faculty

from FAANG/IITs/TOP MNC's

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

1 comments on “Find common elements In 3 sorted arrays in Python”


  • bvschandrika2

    def find_common_elements(arr1, arr2, arr3):
    i = j = k = 0
    common = []

    while i < len(arr1) and j < len(arr2) and k < len(arr3):
    # If all three are equal
    if arr1[i] == arr2[j] == arr3[k]:
    if not common or common[-1] != arr1[i]: # Avoid duplicates
    common.append(arr1[i])
    i += 1
    j += 1
    k += 1
    # Find the smallest and move its pointer forward
    elif arr1[i] < arr2[j]:
    i += 1
    elif arr2[j] < arr3[k]:
    j += 1
    else:
    k += 1

    return common