UNIQUE NUMBERS of array

Suppose you have an array -[1 2 2 3 2 4 5] we have to find the no of unique numbers in the array. ans would be {4(1,3,4,5)} . Can we do this using bit manipulation?

hello @samardeep
if range of number is very small then u can use bitmanipulation .

for example->
1 2 2
here the u can set the bits present at these value as 1 and remaining posiiton as 0. and then number of set bits will be number of unique numbers.

-> 0110
-> 3210

I didn’t get. for this eg [1,2 ,2 ,3, 2 ,4 ,5] , 1- 001, 2- 010, 2-010, 3-011, 4- 100, 5- 101. then which set bit you are talking about and also very small range of numbers, means upto which?

u need to update one single variable whose value will be 0 initially.
1- 000001
2-000011
2-000011
3-000111
…
so on
the position of set bits are indicating what values are present.

upto 63 if using long long.
otherwise upto 31

I am not getting what u r trying to explain. Please explain it again using this case [1,2,3,2,3,3,6,7] and the ans is 3.

consider this small case [1 , 3, 3]
initilally take variable with initial value 0000000000(zero).

now iterate the Array.
1 -> set the bit at position 1 (note right most bit is position 0) (000010 )
3-> set the bit at position 3 (001010)
3-> set the bit at position 3 (001010)
now the number of set bit in (001010 ) is no of distinct numbers

in 1,3,3 no of distinct no is one(1 as 3 is getting repeated twice)

…

oh so u want that numbers that are only repeated once.
use map or other technique.