Given problem:
0/1-Knapsack problem, n items each having weight w_i and value v_i. Find the maximum total value of items whose weights sum up to weight W.
But there are two constraits:
The total weight of all items in the knapsack need to be exactly W.
The total amount of items must be even.
I want to find an algorithm that pays attention to both constraits. I already found out how I can pay attention to one of them at one time.
Here is my implementation which pays attention to constrait 1 (exact weight W):
public class KnapSackExactWeight {
public static void main(String[] args) {
int[] w = new int[] {4, 1, 5, 8, 3, 9, 2}; //weights
int[] v = new int[] {2, 12, 8, 9, 3, 4, 3}; //values
int n = w.length;
int W = 10; // W (max weight)
int[][] DP = new int[n+1][W+1];
for(int i = 1; i < n+1; i++) {
for(int j = 0; j < W+1; j++) {
if(i == 0 || j == 0) {
DP[i][j] = 0;
} else if (j - w[i-1] >= 0) {
DP[i][j] = Math.max(DP[i-1][j], DP[i-1][j - w[i-1]] + v[i-1]);
} else {
DP[i][j] = -Integer.MAX_VALUE;
}
}
}
System.out.println("Result: " + DP[n][W]);
}
}
Result: 22
And here is my implementation which takes constrait 2 into account (even amount of items):
public class KnapSackEvenAmount {
public static void main(String[] args) {
int[] weights = new int[] {4, 1, 5, 8, 3, 9, 2}; //weights
int[] values = new int[] {2, 12, 8, 9, 3, 4, 3}; //values
int n = weights.length;
int W = 10;
int[][] DP_odd = new int[n+1][W+1];
int[][] DP_even = new int[n+1][W+1];
for(int i = 0; i < n+1; i++) {
for(int j = 0; j < W+1; j++) {
DP_even[i][j] = -1;
DP_odd[i][j] = -1;
if(i == 0 || j == 0) {
DP_odd[i][j] = -1;
DP_even[i][j] = 0;
} else if(j - weights[i-1] >= 0) {
if(DP_odd[i-1][j - weights[i-1]] >= 0) {
DP_even[i][j] = Math.max(DP_even[i-1][j], DP_odd[i-1][j - weights[i-1]] + values[i-1]);
}
if(DP_even[i-1][j - weights[i-1]] >= 0) {
DP_odd[i][j] = Math.max(DP_odd[i-1][j], DP_even[i-1][j - weights[i-1]] + values[i-1]);
}
}
if(i > 0) {
DP_odd[i][j] = Math.max(DP_odd[i][j], DP_odd[i-1][j]);
DP_even[i][j] = Math.max(DP_even[i][j], DP_even[i-1][j]);
}
}
}
System.out.println("Result: " + DP_even[n][W]);
}
}
Result: 21
The idea for that: I use two DP tables (DP_even and DP_odd) and save the best solution for a knapsack with odd amount of items in DP_odd and for one with an even amount of items in DP_even.
Now my Problem is how to implement that both constraits work together. Is there a way to solve this?
(If anything is unclear in my question, just ask!)
Not sure if it's the best way to do this problem but what I've done here is to initially reduce the problem to fit the constraints. First find the possible even number of items that weigh equal to the knapsack weight and then find the combination with highest value
import java.util.Scanner;
import static java.lang.Math.pow;
public class subSet{
void subset(int num,int n, int x[])
{
int i;
for(i=1;i<=n;i++)
x[i]=0;
for(i=n;num!=0;i--)
{
x[i]=num%2;
num=num/2;
}
}
public static void main(String[] args) {
int n,d,sum,present=0;
int j;
System.out.println("enter the number of items");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int a[]=new int[n+1];
int x[]=new int[n+1];
System.out.println("enter the weights of items");
for(int i=1;i<=n;i++)
a[i]=sc.nextInt();
System.out.println("enter the values of items");
int v[]=new int[n+1];
for(int i=1;i<=n;i++)
v[i]=sc.nextInt();
System.out.println("enter the max weight");
d=sc.nextInt();
int sol=0;int max=0;
if(d>0)
{
for(int i=1;i<=Math.pow(2,n)-1;i++)
{
subSet s=new subSet();
s.subset(i,n,x);
sum=0;int count=0;
for(j=1;j<=n;j++)
if(x[j]==1)
{
sum=sum+a[j];
count++;
}
sol=0;
if(d==sum && count%2==0)
{
present=1;
for(j=1;j<=n;j++)
{
if(x[j]==1)
sol=v[j]+sol;
if(sol>max)
max=sol;
}
}
}
}
if(present==0)
System.out.println("Solution does not exists");
else
System.out.print("solution = "+max);
}
}
I think the problem might be this line:
DP_odd[i][j] = -1;
You give a penalty of only 1 for using an odd amount of times.
If you simply increase this to a larger negative number (e.g. max negative value for integers) I think your current algorithm should work.
Related
I have the following codes and instead of typing the numbers using scanner function. I want to use an array instead . How do I do this? I need help. Thanks in advance
public class salomon {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
int[] minHeap = new int[n];
int[] maxHeap = new int[n];
int minHeapSize = 0;
int maxHeapSize = 0;
float currentMedian = 0;
for (int a_i = 0; a_i < n; a_i++) {
a[a_i] = in.nextInt();
if (a[a_i] < currentMedian) {
maxHeap[maxHeapSize++] = a[a_i];
// making sure the max heap has maximum value at the top
if (maxHeap[maxHeapSize - 1] > maxHeap[0]) {
swap(maxHeap, maxHeapSize - 1, 0);
}
} else {
minHeap[minHeapSize++] = a[a_i];
// making sure the min heap has minimum value at the top
if (minHeap[minHeapSize - 1] < minHeap[0]) {
swap(minHeap, minHeapSize - 1, 0);
}
}
// if the difference is more than one
if (Math.abs(maxHeapSize - minHeapSize) > 1) {
if (maxHeapSize > minHeapSize) {
swap(maxHeap, maxHeapSize - 1, 0);
minHeap[minHeapSize++] = maxHeap[--maxHeapSize];
swap(minHeap, 0, minHeapSize - 1);
buildMaxHeap(maxHeap, maxHeapSize);
} else {
swap(minHeap, minHeapSize - 1, 0);
maxHeap[maxHeapSize++] = minHeap[--minHeapSize];
swap(maxHeap, 0, maxHeapSize - 1);
buildMinHeap(minHeap, minHeapSize);
}
}
// calculate the median
if (maxHeapSize == minHeapSize) {
currentMedian = (minHeap[0] + maxHeap[0]);
currentMedian = currentMedian / 2;
} else if (maxHeapSize > minHeapSize) {
currentMedian = maxHeap[0];
} else {
currentMedian = minHeap[0];
}
System.out.println(currentMedian);
}
}
static void buildMaxHeap(int[] input, int heapSize) {
int depth = (heapSize - 1) / 2;
for (int i = depth; i >= 0; i--) {
maxHeapify(input, i, heapSize);
}
}
static void maxHeapify(int[] input, int i, int heapSize) {
int left = 2 * i + 1;
int right = 2 * i + 2;
// find the largest
int largest = i;
if (left < heapSize && input[left] > input[largest]) {
largest = left;
}
if (right < heapSize && input[right] > input[largest]) {
largest = right;
}
if (largest != i) {
//swap
swap(input, i, largest);
//recursive call
maxHeapify(input, largest, heapSize);
}
}
static void buildMinHeap(int[] input, int heapSize) {
int depth = (heapSize - 1) / 2;
for (int i = depth; i >= 0; i--) {
minHeapify(input, i, heapSize);
}
}
static void minHeapify(int[] input, int i, int heapSize) {
int left = 2 * i + 1;
int right = 2 * i + 2;
// find the smallest
int smallest = i;
if (left < heapSize && input[left] < input[smallest]) {
smallest = left;
}
if (right < heapSize && input[right] < input[smallest]) {
smallest = right;
}
if (smallest != i) {
//swap
swap(input, i, smallest);
//recursive call
minHeapify(input, smallest, heapSize);
}
}
static void swap(int[] input, int i, int j) {
if (i == j)
return;
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
When I typed 6, 12, 4, 5, 3, 8, 7 and enter - I get the Moving median
12.0
8.0
5.0
4.5
5.0
6.0
I want to replace the scanner with an array {6, 12, 4, 5, 3, 8, 7}`
instead and how do I adjust the code. Thanks
The first value 6 is the array length. Assuming you intend for that to remain, you would remove everything that "isn't an elephant" (namely all of the median calculations), and rename int[] a to int[] f. Like,
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] f = new int[n];
for (int i = 0; i < n; i++) {
f[i] = in.nextInt();
}
System.out.println(Arrays.toString(f));
}
When executed with your example input, this outputs
[12, 4, 5, 3, 8, 7]
If you really did want the six as well, then you would need to add it. Like,
int[] f = new int[n + 1];
f[0] = n;
for (int i = 1; i <= n; i++) {
f[i] = in.nextInt();
}
System.out.println(Arrays.toString(f));
which outputs (as requested)
[6, 12, 4, 5, 3, 8, 7]
I need to be able to have an array of integers that places even integers before odd integers then sort the array in ascending order. For instance given the array [1,2,3,4,5] the final product should look like [2,4,1,3,5] as you can see the array has both the evens before the odds and then places them in ascending order.
As you see in the code I am using the array [10,9,8,7,6,5,4,3,2,1]. The output I am getting is [1,2,3,4,5,6,7,8,9,10], but this is incorrect - it should be [2,4,6,8,10,1,3,5,7,9]. I cannot use any built in functions or libraries. Here is what I have so far:
public class EBOMain {
static int[] Array = { 10,9,8,7,6,5,4,3,2,1};
static int n;
public static void main(String[] args) {
System.out.print("Array before sort: " );
for (int i = 0; i < Array.length; i++)
System.out.print(Array[i] +" ");
n = Array.length;
EvenBeforeOdd(Array, n);
int[] Array2 = new int[Array.length];
MergeSort(Array2, 0,Array.length - 1);
System.out.print("\nArray after sort: " );
for (int i = 0; i < Array.length; i++)
System.out.print(Array[i] +" ");
}
public static void EvenBeforeOdd(int []Array,int n){
if (n==0)
return;
else if(Array[n-1]%2==0) {
for(int i=0;i<n-1;i++) {
if(Array[i]%2!=0) {
int temp = Array[i];
Array[i]= Array[n-1];
Array[n-1] = temp;
EvenBeforeOdd(Array,n-1);
}
}
}
else
EvenBeforeOdd(Array,n-1);
}
static void MergeSort(int[] Combine, int LowerIndex, int UpperIndex) {
if (LowerIndex == UpperIndex){
return;
}
else { // locate Pivot
int Pivot = (LowerIndex + UpperIndex) / 2;
MergeSort(Combine, LowerIndex, Pivot);
MergeSort(Combine, Pivot + 1, UpperIndex);
Merge(Combine, LowerIndex, Pivot + 1, UpperIndex);
}
}
static void Merge(int[] Array2, int Small, int Large, int UpperIndex) {
int Pivot = Large - 1;
int LowerIndex = Small;
int n = UpperIndex - LowerIndex + 1;
int i = 0;
while (Small <= Pivot && Large <= UpperIndex)
if (Array[Small] < Array[Large])
Array2[i++] = Array[Small++];
else
Array2[i++] = Array[Large++];
while (Small <= Pivot)
Array2[i++] = Array[Small++];
while (Large <= UpperIndex)
Array2[i++] = Array[Large++];
for (i = 0; i < n; i++)
Array[LowerIndex + i] = Array2[i];
}
}
You just need to call MergeSort twice. Determine where the last even number is, call MergeSort on the even numbers, then on the odds. There should be boundary checks in case there are no even numbers (not included here):
EvenBeforeOdd(Array, n);
int lastEven = 0;
for (int i=0; i<Array.length; i++) {
if (Array[i] % 2 == 0)
lastEven = i;
}
int[] Array2 = new int[Array.length];
MergeSort(Array2, 0, lastEven);
MergeSort(Array2, lastEven+1, Array.length - 1);
Below is one of possible solutions of this problem.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
class Main {
private static final int[] array = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
public static void main(String[] args) {
List<Integer> odd = new ArrayList<>();
List<Integer> even = new ArrayList<>();
for (int number : array) {
if (number % 2 != 0)
odd.add(number);
else
even.add(number);
}
Collections.sort(odd);
Collections.sort(even);
List<Integer> evenBeforeOdd = new ArrayList<>();
Stream.of(even, odd).forEach(evenBeforeOdd::addAll);
evenBeforeOdd.forEach(System.out::println);
// 2,4,6,8,10,1,3,5,7,9
}
}
I think I confused the methods and can't get the actual methods correct. Please help, the array needs to rotate to the right where the first digit will become the last digit. I generate a random array based on size that the user input. Then I print that array, then do the rotation, then print the new array with the first digit last and the last digit first.
import java.util.Scanner;
import java.util.Random;
public class modOne {
public static final int MIN = 1;
public static final int MAX = 15;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int arraySize = 0;
System.out.println("How many arrays to rotate right?");
int howMany = input.nextInt();
while(howMany <= 0) {
System.out.println("ERROR! Should be positive. REENTER: ");
howMany = input.nextInt();
}
while(howMany > 0) {
System.out.println("Enter array size: ");
arraySize = input.nextInt();
}
int[] a = new int[arraySize];
int[] b = new int[arraySize];
getRand(a, MIN, MAX);
System.out.println("The array before rotation: ");
printArray(a);
System.out.println("THe array after rotation: ");
transArray(a);
printArray(b);
System.out.println("Enter array size: ");
arraySize = input.nextInt();
}
public static void getRand (int[] list, int size, int low, int up) {
Random rand = new Random();
for(int r = 0; r < size; r++) {
list[r] = rand.nextInt(up - low + 1) + low;
}
}
public static void printArray(int[] list, int size) {
for (int r = 0; r < size; r++) {
System.out.printf("%5d", list[r]);
if(r % 6 == 5)
System.out.println();
}
System.out.println();
}
public static void transArray(int[] list, int size) {
for(int r = 0; r < size - 1; r++) {
list[r] = list[r-1];
}
}
}
You can make use of System.arrayCopy to copy sourceArray from start index to destination array from start index. You can also specify how many elements should e copied as last argument to System.arrayCopy method. See below:
int[] arr = new int[]{8, 15, 2,10, 11,15,1,3};
System.out.println("array before rotation: " + Arrays.toString(arr));
int lastElement = arr[arr.length -1 ];
System.arraycopy(arr, 0, arr, 1, arr.length-1);
arr[0] = lastElement;
System.out.println("array after rotation: " + Arrays.toString(arr));
I backup the last element and then copy the array entirely in a new array skipping the first element of the array and then I assign the first element of the array with the value that I backup early.
The output you get is as follow:
array before rotation: [8, 15, 2, 10, 11, 15, 1, 3]
array after rotation: [3, 8, 15, 2, 10, 11, 15, 1]
For more details on how System.arrayCopy works, you can see the following tutorial or read my answer in this question.
You just have to invert the order and save the value you are going to overwrite to recover it later. I also took the liberty to make some other changes to your code. Now it should work as intended.
while (howMany <= 0) {
System.out.println("ERROR! Should be positive. REENTER: ");
howMany = input.nextInt();
}
System.out.println("Enter array size: ");
arraySize = input.nextInt();
int[] a = new int[arraySize];
int[] b = new int[arraySize];
getRand(a, MIN, MAX);
System.out.println("The array before rotation: ");
printArray(a);
System.out.println("THe array after rotation: ");
printArray(transArray(a));
}
public static void getRand(int[] list, int low, int up) {
Random rand = new Random();
for (int r = 0; r < list.length; r++) {
list[r] = rand.nextInt(up - low + 1) + low;
}
}
public static void printArray(int[] list) {
for (int r = 0; r < list.length; r++) {
System.out.printf("%5d", list[r]);
if (r % 6 == 5) {
System.out.println();
}
}
System.out.println();
}
public static int[] transArray(int[] list) {
int temp1 = list[list.length - 1];
for (int r = list.length - 1; r > 0; r--) {
list[r] = list[r-1];
}
list[0] = temp1;
return list;
}
What you can do is use Arrays.copyOfRange(T[] original, int from, int to) in order to shift most of the array. Some numbers are cut off, though, and need to be placed back in afterwards.
public static void main( String[] args ) {
int[] arr = { 1, 2, 3, 4, 5, 6 };
arr = rot(arr, -1);
System.out.println(Arrays.asList(arr));
}
public static int[] rot( int[] arr, int amt ) {
while ( amt < 0 )
amt += arr.length;
amt = amt % arr.length;
int[] k = Arrays.copyOfRange(arr, amt, arr.length + amt);
for ( int i = 0; i < amt; i++ )
k[k.length - amt + i] = arr[i];
return k;
}
rot() rotates left by amt, so using the parameter amt = -1 achieves what you set out to do.
The rot() method first forces 0 ≤ amt < arr.length. This is because once you rotate by the full length of the array, you're back where you started. Next it shifts the array by amt, adding the integer's default value (0) to the end as padding to preserve length. Finally, it adds the lost elements back into the array where the 0's were placed.
for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
The logic behind this is that you are swapping the values until you get to the center:
I made this image for you to better understand
Here, there is no center, but if there was a 3.5, then it would ignore the three point five because validData[validData.length - i - 1] = temp;
I got the largest number and smallest number from the string. But how do I find second largest number and third largest number in this java code from this problem? which code should i use? Please explain
public class Problem1
{
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// int b[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
Problem1 app = new Problem1();
app.scrambleArray(a);
app.print(a);
// Usage enable assertions: -ea to VM arguments
int result = app.findInt(a, 10);
assert (result == 10) :
String.format("Expected <10> but was <%d>", result);
result = app.findInt(a, 11);
assert (result == -1) :
String.format("Expected <-1> but was <%d>", result);
System.out.printf("Largest Number is : %d%n", app.getMax(a));
app.print(app.reverseArray(a));
}
public void scrambleArray(int[] a) {
for (int i = 0; i < a.length; i++) {
int pos = new Random().nextInt(a.length);
int tmp = a[i];
a[i] = a[pos];
a[pos] = tmp;
}
}
public void print(int[] a) {
System.out.println(Arrays.toString(a));
}
public int getMax(int[] a) {
int max = a[0];
for (int i = 1; i < a.length; i++) {
max = Math.max(a[i], max);
}
return max;
}
public int findInt(int[] a, int value) {
int result = -1;
for (int i : a) {
if (value == i) {
result = value;
break;
}
}
return result;
}
public int[] reverseArray(int[] a) {
int[] results = new int[a.length];
for (int i = 0, idx = a.length - 1; i < a.length; i++, idx--) {
results[i] = a[idx];
}
return results;
}
}
Use Arrays.sort() method to sort your integer array
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Arrays.sort(a);
System.out.println("largest value: " + a[a.length - 1]);
System.out.println("second largest: " + a[a.length - 2]);
The prior answer is generally a good solution. The exception is if the number of elements in the array is very big and performance matters. In that case, it may be faster to keep the N largest elements in a sorted set, and avoid sorting the whole list:
public int[] getNLargest(int[] in, int n){
TreeSet<Integer> large = new TreeSet<Integer>();
for(int i : in){
if(large.size() < n){
large.add(i);
} else if(i > large.first().intValue()){
large.remove(large.first());
large.add(i);
}
}
int[] result = new int[large.size()];
int index = 0;
for(Integer i : large){
result[index] = i.intValue();
index++;
}
return result;
}
import java.util.*;
public class SecondLargestInArray
{
public static void main(String[] args)
{
int arr[] = {14,46,47,86,92,52,48,36,66,85,92};
int largest = arr[0];
int secondLargest = arr[0];
System.out.println("The given array is:" );
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+"\t");
}
for (int i = 0; i < arr.length; i++)
{
if (arr[i] > largest)
{
secondLargest = largest;
largest = arr[i];
}
else if((arr[i]<largest && arr[i]>secondLargest) || largest==secondLargest)
{
secondLargest=arr[i];
}
}
System.out.println("\nLargest number is:" + largest);
System.out.println("\nSecond largest number is:" + secondLargest);
}
}
Have a program where the user inputs 10 int values into the array. Lastly I need to pull out the distinct values and display them. Added my second for loop which would determine if the the value is distinct (i.e. meaning if the number appears multiple times it is only displayed once).
For instance, let say I pass in the numbers: 1, 2, 3, 2, 1, 6, 3, 4, 5, 2 the distinct array should only contain numbers {1, 2, 3, 6, 4, 5}
import java.util.Scanner;
import java.io.*;
public class ArrayDistinct {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
// Create arrays & variables
int arrayLength = 10;
int[] numbers = new int[arrayLength];
int[] distinctArray = new int[arrayLength];
int count = 0;
System.out.println("Program starting...");
System.out.print("Please enter in " + numbers.length + " numbers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextInt();
}
for (int i = 0; i < numbers.length; i++) {
int temp = numbers[i];
int tempTwo = numbers[i + 1];
if (tempTwo == temp) {
count++;
distinctArray[i] = temp;
}
}
// Print out results
} // end main
} // end class
Try this :
Set<Integer> uniqueNumbers = new HashSet<Integer>(Arrays.asList(numbers));
uniqueNumbers will contain only unique values
In Java 8
Stream< T > distinct()
Returns a stream consisting of the distinct elements (according to
Object.equals(Object)) of this stream. For ordered streams, the
selection of distinct elements is stable (for duplicated elements, the
element appearing first in the encounter order is preserved.) For
unordered streams, no stability guarantees are made.
Code:
Integer[] array = new Integer[]{5, 10, 20, 58, 10};
Stream.of(array)
.distinct()
.forEach(i -> System.out.print(" " + i));
Output:
5,10,20,58
Read More About distinct function
Try this code.. it will work
package Exercises;
import java.util.Scanner;
public class E5Second
{
public static void main(String[] args)
{
Scanner In = new Scanner(System.in);
int [] number = new int [10];
fillArr(number);
boolean [] distinct = new boolean [10];
int count = 0;
for (int i = 0; i < number.length; i++)
{
if (isThere(number,i) == false)
{
distinct[i] = true;
count++;
}
}
System.out.println("\nThe number of distinct numbers is " + count);
System.out.print("The distinct numbers are: ");
displayDistinct(number, distinct);
}
public static void fillArr(int [] number)
{
Scanner In = new Scanner(System.in);
System.out.print("Enter ten integers ");
for (int i = 0; i < number.length; i++)
number[i] = In.nextInt();
}
public static boolean isThere(int [] number, int i)
{
for (int j = 0; j < i; j++)
if(number[i] == number[j])
return true;
return false;
}
public static void displayDistinct(int [] number, boolean [] distinct)
{
for (int i = 0; i < distinct.length; i++)
if (distinct[i])
System.out.print(number[i] + " ");
}
}
One possible logic: If you're supposed to only sort out "unique" numbers, then you'll want to test each number as it's entered and added to the first array, and loop through the array and see if it's equal to any of the numbers already there; if not, add it to the "unique" array.
Sets in java doesn't allow duplicates:
Integer[] array = new Integer[]{5, 10, 20, 58, 10};
HashSet<Integer> uniques = new HashSet<>(Arrays.asList(array));
That's it.
Something like this should work for you:
Scanner input = new Scanner(System.in);
// Create arrays & variables
int arrayLength = 10;
int[] numbers = new int[arrayLength];
int[] distinctArray = new int[arrayLength];
int count = 0;
Set<Integer> set = new HashSet<Integer>();
System.out.println("Program starting...");
System.out.print("Please enter in " + numbers.length + " numbers: ");
for (int i = 0; i < numbers.length; i++) {
set.add(input.nextInt());
}
for(Integer i : set)
{
System.out.println("" + i);
}
This will only add unique values to the set.
int a[] = { 2, 4, 5, 3, 3, 3, 4, 6 };
int flag = 0;
for (int i = 0; i < a.length; i++)
{
flag = 0;
for (int j = i + 1; j < a.length; j++)
{
if (a[i] == a[j])
{
flag = 1;
}
}
if (flag == 0)
{
System.out.println(a[i]);
}
}