/*
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
- Java.Collections.What is the difference between java.util.Collection and java.util.Collections?
- Java.Collections.
- Java.Collections.Arrange the following interfaces in a hierarchy: List, Set, Map, SortedSet, SortedMap, Collection, Iterable, Iterator, NavigableSet, NavigableMap.
- Java.Collections.Why do i need insertion order and what is difference to sortedOrder ?
- Java.What is LinkedHashSet ?
- Java.Collections.What is SortedSet ?
- Java.Collections.Arrange the following interfaces in a hierarchy: List, Set, Map, SortedSet, SortedMap,
- Java.Collections.Name the main JCF interfaces and their implementations.
- Java.Collections.What is a “collection”?
- Java.Algo.1442. Count Triplets That Can Form Two Arrays of Equal XOR
- Java.Core.What is “internationalization”, “localization” in Java ?
- Java.Core.What are generics?
- Java.Core.Suppose there is a method that can throw IOException and FileNotFoundException, in what order should the catch blocks go? How many catch blocks will be executed?
- Java.Core.Can the main method throw an exception outside and if so, where will this exception be handled?
- Java.Core.Is the finally block always executed?
- Java.Core.Can one catch block catch several exceptions at once?
- Java.Core.Is it possible to use a try-finally block (without catch)?
- Java.Core.What is the try-with-resources mechanism?
- Java.Core.Describe the operation of the try-catch-finally block.
- Java.What do you know about OutOfMemoryError?
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)
- Регулярные выражения
- Тестирование приложений