1

Given an integer array/list(ARR) of size N. Where N is equal to [2M + 1]. Now, in the given array/list, 'M' numbers are present twice and one number is present only once. Find and return that number that is unique in the array/list.

Note: Unique element is always present in the array/list according to the given condition.

I have done this question using WHILE loop, but when I am trying to do this using FOR loop it is not working

and, I have seen some codes for this type of problem, but as I am a learner and have done only some basic python so and don't know yet about the COMPLEXITY(O^N).

please help and thanks in advance

WHILE loop code:


def find_unique(li,n):
    for i in range(n):
        j = 0
        while(j < n):
            if i != j:
                if li[i] == li[j]:
                    break
            j = j + 1
        if j == n:
            return li[i]
n = 7
li = [2,3,1,6,3,6,2]
ele = find_unique(li,n)
print(ele)

FOR loop code:

def find_unique(li,n):
    for i in range(n):
        for j in range(0,n):
            if i == j:
                continue
            if(li[i] == li[j] and j ==n):
                    return li[i]
        

n = 7
li = [2,3,1,6,3,6,2]
ele = find_unique(li,n)
print(ele)


6
  • 2
    convert to numpy.ndarray or a pandas dataframe . They both have a function for it. numpy.org/doc/stable/reference/generated/numpy.unique.html and pandas.pydata.org/docs/reference/api/pandas.unique.html . You can look at some examples in the documentation. Python is slow in for loops, so, whenever you can, convert to numpy array and use the functions of that as they're quite fast. It's called vectorization if you want to read more about it. Commented May 23, 2021 at 10:12
  • In the for-loop, will j ever reach n? Commented May 23, 2021 at 10:12
  • What purpose does the j==n serve exactly? Commented May 23, 2021 at 10:14
  • @SinaMeftah it is not relevant to the question as the author wants to find the issue in code. Commented May 23, 2021 at 10:14
  • @DeepakGouda yes, i know j is not reaching n . but, how to correct it. Commented May 23, 2021 at 10:15

8 Answers 8

3
def unique(arr,N):
    ans=0
    for i in range(N):
        ans=ans^arr[i]
    return ans
Sign up to request clarification or add additional context in comments.

4 Comments

I like this answer - I knew there had to be a mathematical way to do this. (BTW you can simplify this by iterating over the list directly. "for n in arr: ans = ans ^ n")
I have to say this idea is great, but it doesn't seem to be all that satisfying. For example, [2, 3, 6, 3, 6, 2, 3, 11].
@pppig your test case is fine but in the question it is already provided every numbers are present in pairs and there is only one unique value, but in your case 3 is present 3 times which is violating the problem statement . Hope I was able to answer your query.
@AdityaVikram Oh, you are right, after reading your answer, I can not help but want to understand its thinking, so that I forget the description of the question, this idea is really great.
1
from collections import Counter

li = [2, 3, 1, 6, 3, 6, 2]
print([key for key, val in Counter(li).items() if val == 1])

# [1]

Comments

0

I just replaced some code

def find_unique(li,n):
    for i in range(n):
        for j in range(0,n):
            if i != j:
                if li[i] == li[j]:
                    break
            j = j + 1
        if j == n:
            return li[i]
n = 7
li = [2,3,1,6,3,6,2]
ele = find_unique(li,n)
print(ele)

1 Comment

thanks, for the answer. i just want to know why there is j = j + 1, if that is being done by the for loop
0
from collections import Counter
def find_unique(li,n):
    a = Counter(li).most_common()
    return a[len(a)-1][0]    
n = 7
li = [2,3,1,6,3,6,2]
ele = find_unique(li,n)
print(ele)

Comments

0

Since this is a very specific case - with all items repeating twice but one. You can pull off a simple one-liner using this property

find_unique = lambda li, n: 2*sum(set(li)) - sum(li)

Once you have defined the function - Now use it

n = 7
li = [2,3,1,6,3,6,2]
ele = find_unique(li, n)
print(ele)

Comments

0

JAVA CODE:

public class Solution{

public static int findUnique(int[] arr){
    
    int n = arr.length;
    int similar = arr[0]; //first element of array is stored as similar
    //now compare similar with every element of array
    for(int i=1; i<=n-1; i++)
    {
        similar = similar ^ arr[i];
        //XOR operation 1^1 || 0 ^0 = 0
        //XOR operation 1^0 || 0^1= 1
        
        //therefore, similar ^ similar = 0
        //similar ^ unique  = unique
        //similar takes the value of unique after XOR
    }
    
    return similar; //unique element is returned using similar named variable
}

}

Comments

0

Check this code:

def find_unique(li,n):
    unique_numbers = list(set(li))
    for i in unique_numbers:
        if li.count(i) == 1:
            return i
    return "No number once"

It return the number that is once in the array. Size N is not necessary. Set and Count are functions from list variables. Set return unique elements in the array and count return the number of times an element is in the array.

Comments

0

From an array, to find unique values with its count; just use np.unique function as follows:

unique_values = np.unique(any_2d_array.flatten(), return_counts=True)

For example after counting an image (of mask), I got the result. That means the array contains 3 unique values 1, 2, 3 and these appears 22938, 198766 and 18296 times, respectively.enter image description here

To get count into a separate variable, you can use like this:

unique_values, count = np.unique(any_2d_array.flatten(), return_counts=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.