Skip to content

Instantly share code, notes, and snippets.

@Deivur
Created December 25, 2015 10:19
Show Gist options
  • Save Deivur/6d1f6c9d304e1be7547f to your computer and use it in GitHub Desktop.
Save Deivur/6d1f6c9d304e1be7547f to your computer and use it in GitHub Desktop.
Поиск числа в массиве без пары. Генерация файла в YAML формате
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