im having a problem on how to remove duplicate value on an array,can anyone help me?
I have this code made to just merge 2 arrays and displaying the result but i dont know how to remove the duplicate inputs. (im sorry i just started learning java.)
public static void main(String... args) {
int[] x = new int[3];
int[] y = new int[3];
int[] xy = new int[6];/***new array to hold the integers of arrays x and y ****/
int temp, c = 0;
Scanner myInput = new Scanner(System.in);
/*** input of array x at the same time storing the integers to array xy ***/
System.out.println("enter 3 integers for array x:");
for (int a = 0; a < 3; a++) {
x[a] = myInput.nextInt();
xy[c] = x[a];
c++;
}
/*** input of array y at the same time storing the integers to array xy ***/
System.out.println("enter 3 integers for array y:");
for (int b = 0; b < 3; b++) {
y[b] = myInput.nextInt();
xy[c] = y[b];
c++;
}
/*sorting...*/
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5 - i; j++) {
if (xy[j] > xy[j + 1]) {
temp = xy[j];
xy[j] = xy[j + 1];
xy[j + 1] = temp;
}
}
}
/*printing of array xy sorted*/
for (int w = 0; w < 6; w++)
System.out.print(xy[w] + " ");
}
Since you have sorted your merged array a simplified solution is to not remove the duplicate but instead filter them out when printing by comparing the current value in the loop to the previous
System.out.print(xy[0] + " ");
for (int w = 1; w < 6; w++) {
if (xy[w] != xy[w - 1]) {
System.out.print(xy[w] + " ");
}
}
Related
I am taking 10 elements and performing a bubble sort on them. I want to add an algorithm that repeats the sort until no swaps are needed to make this more efficient.
Essentially I want to:
repeat until no swaps done in a pass
For elements 1 to (n-1)
compare contents of element value 1 with the contents of the next value
if value 1 is greater than value 2
then swap the values
This is what I have done so far :
{
//create array
int[] iList = new int[10];
Scanner sc = new Scanner(System.in);
//takes in array input for 10 numbers
System.out.println("Enter a array of numbers ");
for(int i = 0; i< 10; i++ )
{
int num = i + 1;
System.out.println("Enter number " + num);
iList[i] = sc.nextInt();
}
//Bubble sorts the array
System.out.println("The array =");
for(int a = 0; a < iList.length; a++ )
{
for(int b = a+1; b < iList.length; b++)
{
if(iList[a] > iList[b])
{
int iTemp = iList[a];
iList[a] = iList[b];
iList[b] = iTemp;
}
System.out.println("Progress = " + Arrays.toString(iList) );
}
}
} ```
Here is my implementation :
public static void sort(int[] nums) {
boolean isSwapped;
int size = nums.length - 1;
for (int i = 0; i < size; i++) {
isSwapped = false;
for (int j = 0; j < size - i; j++) {
if (nums[j] > nums[j+1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
isSwapped = true;
}
}
if (!isSwapped) break;
}
System.out.println("Sorted Array: " + Arrays.toString(nums));
}
I am looking to modify a bubble sort that I used for a one-dimensional array in order for it to sort a two-dimensional array. I honestly am just having a pretty hard time figuring out the necessary changes. My code is below.
Any help/feedback is appreciated, thanks!
int n;
int temp;
System.out.print("Please enter how large you want the array to be: ");
n = in.nextInt();
int sorting[] = new int[n];
System.out.println("Please enter the elements you want in the array: ");
for (int i = 0; i < n; i++)
{
sorting[i] = in.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (sorting[i] > sorting[j])
{
temp = sorting[i];
sorting[i] = sorting[j];
sorting[j] = temp;
}
}
}
System.out.print("Here is your array in ascending order: ");
for (int i = 0; i < n - 1; i++)
{
System.out.print(sorting[i] + ",");
}
System.out.print(sorting[n - 1]);
I have a java code where X is a 2D array and a loop to add data to it. The code is as follows:
public static void main(String [] args0
{
int[] len = new int[3];
double[][] X = null;
double[][] vec = null;
for (int i = 0; i < 3; i++)
{
System.out.println("Enter the len" +(i+1)+":");
len[i] = in.nextInt();
if(i == 0)
{
vec = new double [1][len[i] + 1];
X = new double[1][vec[0].length];
for(int k = 0; k < vec[0].length - 1; k++)
{
Xi[0][k] = 0;
}
Xi[0][ve1[0].length-1] = 1;
}
else
{
vec = new double [1][len[i] + 1];
X = new double[1][vec[0].length];
for(int k = 0; k < vec[0].length - 1; k++)
{
X[0][k] = 0;
}
X[0][vec[0].length-1] = 1;
}
}
}
When I print X, I need it have appended the values added when i=0 and when i>0. But it prints only the value of what is supposedly the final iteration. How do I make it print the data of all iterations appended to the end of data added during each iteration? I understand that since I am creating a new double[][] in each iteration, the value of gets overwritten. But how do i fix it?
You have correctly identified that with X = new double[1][vec[0].length]; you are always overriding the result from previous iterations. In order to fix it, you need to move the initialization out of the for loop. Here is an example of how you can do that:
public static void main(String [] args) {
int[] len = new int[3];
double[][] X = new double[3][];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Enter the len " +(i+1)+":");
len[i] = in.nextInt();
X[i] = new double[len[i]];
for (int j = 0; j < len[i] - 1; j++) {
X[i][j] = 0;
}
X[i][len[i] - 1] = 1;
}
in.close();
}
As you can see, the array X is initialized in the beginning with size 3, since it will always hold 3 one-dimensional arrays in your case. However, you can initialize the size dynamically as well. You don't need the vec variable, because you can always access the length that was just read and stored in len. In fact, you don't need the len array as well, since it stores information that is anyways contained in X:
public static void main(String [] args) {
double[][] X = new double[3][];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Enter the len " + (i+1) + ":");
X[i] = new double[in.nextInt()];
for (int j = 0; j < X[i].length - 1; j++) {
X[i][j] = 0;
}
X[i][X[i].length - 1] = 1;
}
in.close();
}
So I am creating a program that rolls z die x times with y sides, and I keep getting an out of bounds error at the first line in the first for loop. However I'm not sure why this is, the loop counts from 0 to (z-1). I'm basically in the home stretch of this program and I need the help of the stackoverflow community.
public class Ass11f {
public static void main(String[] args) {
EasyReader console = new EasyReader();
System.out.print("Enter how many times you want to roll the die: ");
int x = console.readInt();
System.out.print("Enter the amount of sides: ");
int y = console.readInt();
System.out.print("Enter the amount of die: ");
int z = console.readInt();
int[][] dice = new int[x][z];
int row = 0;
for (int i = 0; i<z; ++i){
dice[row][i] += ((int)(Math.random()*y)+1);
if ((i == z-1)&&(row!=x)) {
i = 0;
++row;
}
}
row = 0;
int[] sum = new int[x];
for (int j = 0; j<z; ++j){
sum[row]+=dice[j][row];
if ((j == z-1)&&(row!=x)) {
j = 0;
++row;
}
}
int[] counter = new int[2*y];
int k = 0;
while (k<sum.length){
for (int l = 0;l<((2*y)-1);++l){
if (sum[k]==l) ++counter[l];
if (l==((2*y)-1)) {
++k;
}
}
}
for (int m = 0;m<sum.length;++m) System.out.println(sum[m]+"'s: "+counter[m]+"times, "+((((double)counter[m])/x)*100)+"%");
}
}
first loop:
for (int i = 0; i<z; i++){
dice[row][i] += ((int)(Math.random()*y)+1);
if ((i == z-1)&&(row!=x-1)) {
i = -1;
++row;
}
}
second loop:
for (int j = 0; j<z; j++){
sum[row]+=dice[j][row];
if ((j == z-1)&&(row!=x-1)) {
j = -1;
++row;
}
}
Third loop: runs forever. I'm not sure what this is trying to achieve, so I can't fix it for you...
There are x rows but you are using z as the row loop
int[][] dice = new int[x][z]; <-- x is the row count
int row = 0;
for (int i = 0; i < z; ++i){ <--- The outer loop is iterating the rows (x),
Here's how to iterate through a 2D array
int[][] dice = new int[x][z];
for (int i = 0; i < x; i++){
for (int j = 0; j < z; j++){
// do something with dice[i][j]
}
}
I asked a question on helping me with this question about a week ago
Java permutations
, with a problem in the print permutation method. I have tidied up my code and have a working example that now works although if 5 is in the 5th position in the array it doesn't print it. Any help would be really appreciated.
package permutation;
public class Permutation {
static int DEFAULT = 100;
public static void main(String[] args) {
int n = DEFAULT;
if (args.length > 0)
n = Integer.parseInt(args[0]);
int[] OA = new int[n];
for (int i = 0; i < n; i++)
OA[i] = i + 1;
System.out.println("The original array is:");
for (int i = 0; i < OA.length; i++)
System.out.print(OA[i] + " ");
System.out.println();
System.out.println("A permutation of the original array is:");
OA = generateRandomPermutation(n);
printArray(OA);
printPermutation(OA);
}
static int[] generateRandomPermutation(int n)// (a)
{
int[] A = new int[n];
for (int i = 0; i < n; i++)
A[i] = i + 1;
for (int i = 0; i < n; i++) {
int r = (int) (Math.random() * (n));
int swap = A[r];
A[r] = A[i];
A[i] = swap;
}
return A;
}
static void printArray(int A[]) {
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " ");
System.out.println();
}
static void printPermutation(int[] p)
{
int n = p.length-1;
int j = 0;
int m;
int f = 0;
System.out.print("(");
while (f < n) {
m = p[j];
if (m == 0) {
do
f++;
while (p[f] == 0 && f < n);
j = f;
if (f != n)
System.out.print(")(");
}
else {
System.out.print(" " + m);
p[j] = 0;
j = m - 1;
}
}
System.out.print(" )");
}
}
I'm not too crazy about
int n = p.length-1;
followed by
while (f < n) {
So if p is 5 units long, and f starts at 0, then the loop will be from 0 to 3. That would seem to exclude the last element in the array.
You can use the shuffle method of the Collections class
Integer[] arr = new Integer[] { 1, 2, 3, 4, 5 };
List<Integer> arrList = Arrays.asList(arr);
Collections.shuffle(arrList);
System.out.println(arrList);
I don't think swapping each element with a random other element will give a uniform distribution of permutations. Better to select uniformly from the remaining values:
Random rand = new Random();
ArrayList<Integer> remainingValues = new ArrayList<Integer>(n);
for(int i = 0; i < n; i++)
remainingValues.add(i);
for(int i = 0; i < n; i++) {
int next = rand.nextInt(remainingValues.size());
result[i] = remainingValues.remove(next);
}
Note that if order of running-time is a concern, using an ArrayList in this capacity is n-squared time. There are data-structures which could handle this task in n log n time but they are very non-trivial.
This does not answer the problem you have identified.
Rather i think it identifies a mistake with your generateRandomPermutation(int n) proc.
If you add a print out of the random numbers generated (as i did below) and run the proc a few times it allows us to check if all the elements in the ARRAY TO BE permed are being randomly selected.
static int[] generateRandomPermutation(int n)
{
int[] A = new int[n];
for (int i = 0; i < n; i++)
A[i] = i + 1;
System.out.println("random nums generated are: ");
for (int i = 0; i < n; i++) {
int r = (int) (Math.random() * (n));
System.out.print(r + " ");
Run the proc several times.
Do you see what i see?
Jerry.