DEV Community

mr.Felistus
mr.Felistus

Posted on

Comparing elements in an Array and eliminating duplicates

I want to compare the elements in an array, eliminate the duplicates using map().. example: array = [1,2,1,3,2]=> finalArray = [1,2,3]

Discussion (4)

Collapse
t0maslb profile image
t0maslb • Edited on

This is how I'd do it in Python:

def delete_dupl(list):
    deleted = {}
    # add each element to a map
    for item in list:
        deleted[item] = 1
    # clear list to replace with the shortened one
    list.clear()
    for key in deleted:
        list.append(key)
    return deleted
print(delete_dupl(<given_list>))

There's probably a better way but adding each item to a map (dict) is enough, as there can't be repeated keys; then pass every key to an array.

Collapse
ezeugoobieze profile image
mr.Felistus Author

Ok..
I don't speak python for now. Am a newbie in JavaScript trying to work on that task.

Thanks for your response...๐Ÿ‘

Collapse
t0maslb profile image
t0maslb

Well, I'm not fluent on JS either but I don't think they're so different at tackling this problem :)

Collapse
kdheeraj280702 profile image
Kdheeraj280702

Use sets that would make it easy