/*
Approach
Using Bit Manipulation -
As we know XOR operation with 0 gives the same number
i.e, a XOR 0 = a
eg, for decimal no. 2=> 2 XOR 0 = 2
in binary, 010 XOR 000 = 010
Also we know that , XOR operation with same number gives 0
i.e, a XOR a = 0
eg, 2 XOR 2 = 0
in binary, 010 XOR 010 = 000
XOR is associative (like sum)
i.e, (2 XOR 3) XOR 4 = 2 XOR (3 XOR 4), So the order doesn't matter in performing XOR operation.
eg, 2^3^4^6 = 3^2^6^4 = 4^2^6^3 ......
So, using these three properties of XOR , we will solve the question. we will take ans variable with 0 as initial value. And then for each element i in array, we will perform the XOR operation of the element with 0, ans will become 0 if the same number is found (as a XOR a = 0) and so after the completion of the loop, only element with no duplicate number will remain and will be returned as ans.
*/
// https://leetcode.com/problems/single-number/description/
public class Solution {
public int SingleNumber(int[] nums) {
int ans=0; //since XOR with 0 returns same number
for(int i=0; i < nums.Length; i++){
ans ^= nums[i]; // ans = (ans) XOR (array element at i)
}
return ans;
}
}
-
MY PROJECTS
-
Recent Posts
- Database.Middle.What are surroget keys ?
- Database.Middle.Explain the difference between OLTP and OLAP.
- Database.Middle.Partitions and sharding, differenes
- Database.Middle.do i see different tables in some sql explorer like dataGrip, for example ?
- Database.Middle.How does database partitioning work?
- Database.Middle.What is a trigger? How and when would you use it?
- Database.Middle.What is the difference between ROW_NUMBER(), RANK(), and DENSE_RANK()?
- Database.Middle.What are CTEs (Common Table Expressions)?
- Database.Middle.If i don’t use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so i take all rows ?
- Database.Middle.WindowFunctions.Explain ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
- Database.Total.Give examples with Sum(), Avg(), Max()
- Database.Middle.What is window function?
- Database.Middle.Pagination.SeekMethod.What if some rows were deleted ?
- Database.Middle.Why, when we seek for pages no shifting from inserts/deletes on other rows ?
- Database.Middle.What is the Seek Method?
- Database.Middle.How would you implement pagination in SQL?
- Database.Middle.What is eventual consistency?
- Database.Middle.Explain the concept of sharding in databases.
- Database.Middle.MaterializedView
- Database.Middle.How can you detect and resolve deadlocks?
Categories
- Aptana
- Azure
- C#
- DataSnap
- DBExpress
- Delphi
- Delphi и сети
- Delphi. Язык программирования
- ExtJS
- FastReport
- FireDAC
- FireMonkey
- GIT
- ICS
- IDE
- IIS
- Indy
- InnoSetup
- javascript
- jQuery
- JSON
- LiveBindings
- MSHTML
- MySQL
- PHP
- REST
- Ribbons
- SMS
- SQL инструкции
- SVN
- TRichView
- UniGui
- WebBroker
- WinAPI
- Windows
- Алгоритмы
- Без рубрики
- Деревья
- Ищу ответ
- Компонентостроение
- Мои компоненты
- Начальный уровень
- Обработка исключений
- Парсинг
- Потоки(Threads)
- Регулярные выражения
- Тестирование приложений