Regardig Daily Coding Challenge - Symmetric Difference 12.05

Hi!
Regarding requirement: “Return values in order they first appear”
I see two possible interpretations:

    A) Index-interleaved: Compare arr1[0] vs arr2[0], then arr1[1] vs arr2[1], etc.
       Example: [1,4,7] & [2,3,5,6] → [1,2,4,3,7,5,6]

    B) Sequential: Process all of arr1 first, then all of arr2
       Example: [1,4,7] & [2,3,5,6] → [1,4,7,2,3,5,6]

Which interpretation is intended?
Thanks!

Please do not put text into code blocks - that makes the text difficult to read.

Can you post the link to the challenge please?

1 Like

I will keep that in mind, thank you.

https://www.freecodecamp.org/learn/daily-coding-challenge/2025-12-05

The tests make it look like it is all unique values from the first array then all unique values from the second

At first glance, yes. But both solutions provide the correct answers :thinking:

Good test example will be:
[1,2,3,4,5] & [10,9,8,7,6]

Where is a specific test case where taking the elements from the second array first is correct?

That’s exactly it. There aren’t any. It’s impossible to say for sure which interpretation is correct…

I don’t see any test cases where using the second array first would be correct, other than the last one, where the first array has no unique elements

I hope this code helps:

def difference(arr1:list, arr2:list) -> list:
    diff = []
    seen = set()
    #Iterating over largest array
    for i in range(max(len(arr1), len(arr2))):
        try:
            #Check if array1 have item in array2
            if arr1[i] not in seen and arr1[i] not in arr2:
                diff.append(arr1[i])
                seen.add(arr1[i])
        except IndexError:
            pass
        try:
            #Check if array2 have item in array1
            if arr2[i] not in seen and arr2[i] not in arr1:
                diff.append(arr2[i])
                seen.add(arr2[i])
        except IndexError:
            pass
    return diff

I don’t know why you would use a try-except? That’s definitely not needed.

I think you’re making your logic less clear by trying to loop over both arrays at the same time and index into values that may not exist.

Yes, that’s right. I can skip this with if-statement, or itertools.zip_longest, or by creating a dictionary with the index values. There are probably other ways. For this I am trying just to use it

I would make two separate loops. Its a bad idea to intentionally index into values you know will be invalid.

I would like for it to be more complex challenge, haha. Just one clear test could solve this complex problem of duality. Not enough info :person_shrugging:

def difference(arr1: list, arr2: list) -> list:
    diff = []
    seen = set()
    for item in arr1:
        if item not in seen and item not in arr2:
            diff.append(item)
            seen.add(item)
    for item in arr2:
        if item not in seen and item not in arr1:
            diff.append(item)
            seen.add(item)
    return diff

I really don’t see what’s confusing. You put in all uniques from the first array, then all uniques from the second.

I get it. My mind thinking in parallel, and “first appear in the input arrays” . That S on the end of arrays getting into my head. Or may be my english is bad, hwo konws

You should never try to index into array entries that don’t exist on purpose.

1 Like

I will keep that in mind too. Question will remain, like sun in the clear sky

import itertools
def difference(arr1, arr2):
    diff = []
    for item in itertools.zip_longest(arr1, arr2):
       if item[0] is not None and item[0] not in arr2:
           diff.append(item[0])
       if item[1] is not None and item[1] not in arr1:
           diff.append(item[1])                      
    return diff             
    
def difference(arr1, arr2):                  
    return [item for item in set(arr1) if item not in arr2] + [item for item in set(arr2) if item not in arr1]