Python Method to Find the Largest Unique Number in an Array


python Python Method to Find the Largest Unique Number in an Array algorithms python

python

Given an array of integers A, return the largest integer that only occurs once. If no integer occurs once, return -1.

Example 1:
Input: [5,7,3,9,4,9,8,3,1]
Output: 8
Explanation:
The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it’s the answer.

Example 2:
Input: [9,9,8,8]
Output: -1

Explanation:
There is no number that occurs only once.

Note:
1 <= A.length <= 2000
0 <= A[i] <= 1000

Count the Frequency of Items in Array/List using collections.Counter in Python

The collections.Counter in Python will return a dictionary object where the keys are the items and values are the frequencies that they appear in the original list/array. For example:

1
2
3
4
> a = [1, 1, "a", 3, "a"]
> b = collections.Counter(a)
> print(b) 
Counter({1: 2, 'a': 2, 3: 1})
> a = [1, 1, "a", 3, "a"]
> b = collections.Counter(a)
> print(b) 
Counter({1: 2, 'a': 2, 3: 1})

The collections need to be imported before use, otherwise you will see “NameError: name ‘collections’ is not defined”

1
import collections 
import collections 

The Python Filter Function (Functional Programming)

The filter is a functional programming function that is often used to filter out items given a condition. The first parameter is a function or lambda, and the second parameter is the list.

1
2
3
4
5
> a = [1,2,3,4,5,6]
> filter(lambda x: x%2==0, a)
<filter object at 0x7f5a24cebd30>
> list(filter(lambda x: x%2==0, a))
[2, 4, 6]
> a = [1,2,3,4,5,6]
> filter(lambda x: x%2==0, a)
<filter object at 0x7f5a24cebd30>
> list(filter(lambda x: x%2==0, a))
[2, 4, 6]

Find Largest Unique Number in the List using Python

Then, we can combine the collections.Counter and filter, and use the max function to return the maximum value.

1
2
3
4
5
class Solution:
    def largestUniqueNumber(self, A: List[int]) -> int:
        freq = collections.Counter(A)
        x = list(filter(lambda x: freq[x] == 1, A))
        return max(x) if len(x) > 0 else -1
class Solution:
    def largestUniqueNumber(self, A: List[int]) -> int:
        freq = collections.Counter(A)
        x = list(filter(lambda x: freq[x] == 1, A))
        return max(x) if len(x) > 0 else -1

The lambda function is a short function in Python, and we take parameter x and check if the frequency is one:

1
lambda x: freq[x] == 1
lambda x: freq[x] == 1

The C++ solutions are here: How to Find the Largest Unique Number in Array?
The Javascript solution using Functional Programming style is presented here: How to Find the Largest Unique Number in Array using Javascript (Functional Programming)

Largest Unique Numbers Algorithms/Implementations

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
630 words
Last Post: How to Find the Largest Unique Number in Array using Javascript (Functional Programming)
Next Post: How to Flatten 2D Vector in C++?

The Permanent URL is: Python Method to Find the Largest Unique Number in an Array

Leave a Reply