0

Problem:

There is an array with some numbers. All numbers are equal except for one. Try to find it!

find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55

It’s guaranteed that array contains at least 3 numbers.

The tests contain some very huge arrays, so think about performance.

I want to know in what ways I can increase performance. I've found out that using set() could increase performance, but I don't understand why.

My code:

def find_uniq(arr):
    for i in arr:
        if arr.count(i)==1:
            return i

It will time out with my code.

wovano
  • 4,543
  • 5
  • 22
  • 49
TONY MG
  • 11
  • 1
  • Sets are built on an idea called hashing that enables faster searching than what you can get with a list. The book _Grokking Algorithms_ (https://www.manning.com/books/grokking-algorithms) gives a good intro to the idea and related ones. – ndc85430 Jan 01 '23 at 07:11

1 Answers1

0

The problem is that the time complexity of your code is O(n^2). Your code iterates over all numbers in the list, and for each number arr.count() is called. The method arr.count() will have to iterate the full list again to count the number of times the item occurs. So if the list contains n numbers, this code will have to perform n * n operations. This means that if the list will become 1000 times larger, it will take 1000000 times longer to solve. In coding problems like this, the test set usually contains at least one list that is so long that it will timeout when using such algorithm, but not when using a more optimized algorithm.

In this specific programming exercise, you know that each list contains only 2 unique numbers. One number occurs exactly 1 time, the other number occurs (n - 1) times. You could use a set to find the 2 unique numbers in O(n) time and then iterate over only those 2 items instead of over all n items:

def find_uniq(arr):
    for number in set(arr):  # using set here!
        if arr.count(number) == 1:
            return number

The correctness and performance of this function can be tested with the following code:

# Verify code with example input:
find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55

# Verify performance using a huge array (~1M elements):
huge_array = [1] * 1000000 + [2]
find_uniq(huge_array) == 2
wovano
  • 4,543
  • 5
  • 22
  • 49