Skip to main content
deleted 2 characters in body
Source Link
Sriv
  • 2.8k
  • 2
  • 14
  • 37

You can use collections.Counter to solve this problem with a time complexity of O(N) and a space complexity of also O(N).

from collections import Counter

my_array = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
my_counter = Counter(my_array)

# Thanks to @AlexeyBurdin and @Graipher for improving this part.
print(next(k for k, v in my_counter.items() if v % 2))

This will print out all the elementsfirst element which occur foroccurs an odd number of times.

You can read more about collections.Counter here.

This is the simplest and fastest solution I can think of.

You can use collections.Counter to solve this problem with a time complexity of O(N) and a space complexity of also O(N).

from collections import Counter

my_array = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
my_counter = Counter(my_array)

# Thanks to @AlexeyBurdin and @Graipher for improving this part.
print(next(k for k, v in my_counter.items() if v % 2))

This will print out all the elements which occur for an odd number of times.

You can read more about collections.Counter here.

This is the simplest and fastest solution I can think of.

You can use collections.Counter to solve this problem with a time complexity of O(N) and a space complexity of also O(N).

from collections import Counter

my_array = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
my_counter = Counter(my_array)

# Thanks to @AlexeyBurdin and @Graipher for improving this part.
print(next(k for k, v in my_counter.items() if v % 2))

This will print out the first element which occurs an odd number of times.

You can read more about collections.Counter here.

This is the simplest and fastest solution I can think of.

added 63 characters in body
Source Link
Sriv
  • 2.8k
  • 2
  • 14
  • 37

You can use collections.Counter to solve this problem with a time complexity of O(N) and a space complexity of also O(N).

from collections import Counter

my_array = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
my_counter = Counter(my_array)

# Thanks to @AlexeyBurdin and @Graipher for improving this part.
print([keynext(k for keyk, v in my_counter.items() if my_counter[key]v % 2]2))

This will print out all the elements which occur for an odd number of times.

You can read more about collections.Counter here.

This is the simplest and fastest solution I can think of.

You can use collections.Counter to solve this problem with a time complexity of O(N) and a space complexity of also O(N).

from collections import Counter

my_array = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
my_counter = Counter(my_array)

print([key for key in my_counter if my_counter[key] % 2])

This will print out all the elements which occur for an odd number of times.

You can read more about collections.Counter here.

This is the simplest and fastest solution I can think of.

You can use collections.Counter to solve this problem with a time complexity of O(N) and a space complexity of also O(N).

from collections import Counter

my_array = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
my_counter = Counter(my_array)

# Thanks to @AlexeyBurdin and @Graipher for improving this part.
print(next(k for k, v in my_counter.items() if v % 2))

This will print out all the elements which occur for an odd number of times.

You can read more about collections.Counter here.

This is the simplest and fastest solution I can think of.

Source Link
Sriv
  • 2.8k
  • 2
  • 14
  • 37

You can use collections.Counter to solve this problem with a time complexity of O(N) and a space complexity of also O(N).

from collections import Counter

my_array = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
my_counter = Counter(my_array)

print([key for key in my_counter if my_counter[key] % 2])

This will print out all the elements which occur for an odd number of times.

You can read more about collections.Counter here.

This is the simplest and fastest solution I can think of.