Created
December 25, 2015 10:19
-
-
Save Deivur/6d1f6c9d304e1be7547f to your computer and use it in GitHub Desktop.
Поиск числа в массиве без пары. Генерация файла в YAML формате
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import org.ho.yaml.YamlEncoder; | |
public class Xor { | |
public static void main(String[] args) throws IOException { | |
/* Create array */ | |
int[] arr = { 1, 4, 9, 8, 5, 4, 1, 8, 5 }; | |
/* Create an object File and passed to the constructor path */ | |
File file = new File("E:/file.txt"); | |
/* Checking the existence of the file with the same name */ | |
if (!file.exists()) { | |
file.createNewFile(); | |
} | |
/* Record array to a file */ | |
OutputStream outStream = new FileOutputStream(file); | |
YamlEncoder yamlEncoder = new YamlEncoder(outStream); | |
yamlEncoder.writeObject(arr); | |
yamlEncoder.close(); | |
outStream.close(); | |
/* | |
* Use the properties of XOR: the use of two the same numbers gives zero | |
*/ | |
int notRepeatingElement = arr[0]; | |
for (int i = 1; i < arr.length; i++) { | |
notRepeatingElement ^= arr[i]; | |
} | |
System.out.println(notRepeatingElement); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment