0% found this document useful (0 votes)
39 views

Java Interview Programs

The document discusses a program to find a duplicate number between 1 to N numbers in a list. It contains a DuplicateNumber class with a method to find the duplicate number by calculating the total sum of the list, subtracting the expected sum without a duplicate, and returning the difference. It adds numbers from 1 to 30 to a list, with 22 added twice as the duplicate. Running the findDuplicateNumber method prints the duplicate of 22.

Uploaded by

LÜ ÇKY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Java Interview Programs

The document discusses a program to find a duplicate number between 1 to N numbers in a list. It contains a DuplicateNumber class with a method to find the duplicate number by calculating the total sum of the list, subtracting the expected sum without a duplicate, and returning the difference. It adds numbers from 1 to 30 to a list, with 22 added twice as the duplicate. Running the findDuplicateNumber method prints the duplicate of 22.

Uploaded by

LÜ ÇKY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program: Find out duplicate number between 1 to N numbers.

Code:
package com.java2novice.algos;

import java.util.ArrayList;
import java.util.List;

public class DuplicateNumber {

public int findDuplicateNumber(List<Integer> numbers){

int highestNumber = numbers.size() - 1;


int total = getSum(numbers);
int duplicate = total - (highestNumber*(highestNumber+1)/2);
return duplicate;
}

public int getSum(List<Integer> numbers){

int sum = 0;
for(int num:numbers){
sum += num;
}
return sum;
}

public static void main(String a[]){


List<Integer> numbers = new ArrayList<Integer>();
for(int i=1;i<30;i++){
numbers.add(i);
}
//add duplicate number into the list
numbers.add(22);
DuplicateNumber dn = new DuplicateNumber();
System.out.println("Duplicate Number: "+dn.findDuplicateNumber(numbers));
}
}
List Of All Interview Programs:
Find out duplicate number between 1 to N numbers. Accenture

Find out middle index where sum of both ends are equal.

Write a singleton class.

Write a program to create deadlock between two threads. Accenture

Write a program to reverse a string using recursive algorithm.

Write a program to reverse a number.

Write a program to convert decimal number to binary format.Accenture

Write a program to find perfect number or not.

Write a program to implement ArrayList.

Write a program to find maximum repeated words from a file. Accenture

Wrie a program to find out duplicate characters in a string.

Write a program to find top two maximum numbers in a array. ACCENTURE

Write a program to sort a map by value.

Write a program to find common elements between two arrays.

How to swap two numbers without using temporary variable?

Write a program to print fibonacci series.

Write a program to find sum of each digit in the given number using recursion.

Write a program to check the given number is a prime number or not?

Write a program to find the given number is Armstrong number or not?

Write a program to convert binary to decimal number.

Write a program to check the given number is binary number or not?

Write a program for Bubble Sort in java.

Write a program for Insertion Sort in java.

Write a program to implement hashcode and equals.


How to get distinct elements from an array by avoiding duplicate elements?

Write a program to get distinct word list from the given file.

Write a program to get a line with max word count from the given file.

Write a program to convert string to number without using Integer.parseInt() method.

Write a program to find two lines with max characters in descending order.

Write a program to find the sum of the first 1000 prime numbers.

Find longest substring without repeating characters.

Write a program to remove duplicates from sorted array.

You might also like