Find Missing Number in an Array from 1 to n in Java

Maruthi Krishna
Updated on 02-Aug-2019 13:38:27

17K+ Views

If a single number is missing in an integer array that contains a sequence of numbers values, you can find it basing of the sum of numbers or, basing on the xor of the numbers.Based on the sum of the numbers −The sum of n sequential numbers will be [n*(n+1)]/2. Using this get the sum of the numbers the n numbers.Add all the elements in the array.Subtract the sum of the numbers in the array from the sum of the n numbers.Exampleimport java.util.Scanner; public class MissingNumber {    public static void main(String[] args) {       Scanner sc = ... Read More

Find First Non-Repeating Number in Integer Array using Java

Maruthi Krishna
Updated on 02-Aug-2019 13:34:47

2K+ Views

To find the first non-repeating number in an array −Construct count array to store count of each element in the given array with same length and with initial value 0 for all elements.Compare each element in the array with all other elements, except itself.If match occurs increment its value in the count array.Get the index of the first 0 in the count array and print the element in the input array at this index.Exampleimport java.util.Arrays; public class NonRpeatingArray {    public static void main(String args[]) {       int array[] = {114, 225, 669, 996, 336, 6547, 669, 225, ... Read More

Find First Repeated Element in Integer Array using Java

Maruthi Krishna
Updated on 02-Aug-2019 12:56:21

405 Views

To find the first non-repeating number in an array −Construct count array to store count of each element in the given array with same length and with initial value 0 for all elements.Compare each element in the array with all other elements, except itself.If match occurs increment its value in the count array.Get the index of the first non-zero element in the count array and print the element in the input array at this index.Exampleimport java.util.Arrays; public class NonRpeatingArray {    public static void main(String args[]) {       int array[] = {114, 225, 669, 996, 336, 6547, 669, ... Read More

Reverse a String Without Using Reverse Method in Java

Maruthi Krishna
Updated on 02-Aug-2019 12:27:11

5K+ Views

You can reverse a String in several ways, without using the reverse() function.Using recursion − Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. You can reverse a string using recursive function as shown in the following program.Exampleimport java.util.Scanner; public class StringReverse {    public static String reverseString(String str){       if(str.isEmpty()){          return str;       }else{          return reverseString(str.substring(1))+str.charAt(0);       } ... Read More

Delete Folder and Sub-Folders Using Java

Maruthi Krishna
Updated on 02-Aug-2019 12:13:29

3K+ Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.The delete() method of the File class deletes the file/directory represented by the current File object.This ListFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.Therefore, to delete a folder along with its sub directories and files, you need to define a recursive method.ExampleFollowing Java program deletes the specified directory recursively −import java.io.File; ... Read More

Get File Last Modified Time in Java

Maruthi Krishna
Updated on 02-Aug-2019 12:01:30

3K+ Views

He class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.ExampleFollowing Java program gets the last modified time of a directory −import java.io.File; import java.util.Date; public class GettingLastmodifiedTime {    public static void main(String args[]) {       String filePath = "D://ExampleDirectory//";       //Creating the File ... Read More

List Hidden Files in a Directory Using Java Program

Maruthi Krishna
Updated on 02-Aug-2019 11:47:25

612 Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.The isHidden() method of the File class verifies weather the (abstract path of) file/directory represented by the current File object, is hidden.The ListFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.Therefore, to list all the hidden files in a directory get all the file objects using the ListFiles() method, verify weather each ... Read More

Copy Characters from One File to Another in Java

Maruthi Krishna
Updated on 02-Aug-2019 11:24:50

3K+ Views

To copy the contents of one file to other character by characterExampleimport java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyFiles {    public static void main(String[] args) throws IOException {       //Creating a File object to hold the source file       File source = new File("D:\ExampleDirectory\SampleFile.txt");       //Creating a File object to hold the destination file       File destination = new File("D:\ExampleDirectory\outputFile.txt");       //Creating an FileInputStream object       FileInputStream inputStream = new FileInputStream(source);       //Creating an FileOutputStream object       FileOutputStream outputStream = ... Read More

Get File URI Reference in Java

Maruthi Krishna
Updated on 02-Aug-2019 11:21:24

5K+ Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.In general, an URI (Uniform Resource Identifier) represents a resource. The URI class of Java represents this format You can get the URI format of a file by invoking the toURI() method.ExampleFollowing Java example, prints the URI format of the file named samplefile.txtimport java.io.File; import java.io.IOException; import java.net.URI; public class GetURI {    public static void main(String args[]) throws IOException {       //Instantiating the File class       String ... Read More

Create and Store Property File Dynamically in Java

Maruthi Krishna
Updated on 02-Aug-2019 11:18:19

3K+ Views

The .propertiesis an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.Creating a .properties fileTo create a properties file −Instantiate the Properties class.Populate the created Properties object using the put() method.Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.ExampleThe Following Java program creates a properties file in the path D:/ExampleDirectory/import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile {    public static ... Read More

Advertisements