Setting output in one array - arrays

I need quick help how to put 3 line of output code in one string.
With this code i get answer:
Duplicate Element : 12
Duplicate Element : 0
Duplicate Element : 43
but I want them to be in one array.
public class As1 {
public static void main(String[] args) {
int []array= {12,23,-22,0,43,545,-4,-55,43,12,0,-999,-87};
for (int i = 0; i < array.length-1; i++)
{
for (int j = i+1; j < array.length; j++)
{
if ((array[i] == array[j]) && (i != j))
{
System.out.println("Duplicate Element : "+array[j]);
}
}
}
}
}
I know I can use array.toString[], but I get error.
I want my output to be like this
Duplicate Element : 12, 0, 43.

U can always make one-time print with boolean flag like first:
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
boolean first = true;
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if ((array[i] == array[j]) && (i != j)) {
if(first) {
System.out.print("Duplicate Element:");
first = false;
}
System.out.print(" " + array[j]);
}
}
}
}
Or you can create a new array that has all the duplicates and print them afterwards:
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int[] duplicates = new int[array.length/2]; //maximum duplicates is half of size the given integer array
int k = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if ((array[i] == array[j]) && (i != j)) {
duplicates[k] = array[i];
k++;
}
}
}
System.out.print("Duplicate Element:");
for(int i = 0; i < k; i++) {
System.out.print(" " + duplicates[i]);
}
}

Related

Finding Second duplicate element in an array in java

Hi Everyone, i am new to the programming world, can you please help me in finding second duplicate element in an array. i have tried but its not working.
public class FindSecondDuplicate {
public static void main(String[] args) {
int[] intArray = { 6,4,2,3,4,6,8};
int count=0;
Set<Integer> set=new LinkedHashSet<>();
for(int no:intArray)
{
if(set.add(no)==false)
{
count=count+1;
if(count==2)
{
System.out.println(no);
break;
}
}
else
{
set.add(no);
}
}
}
}
I think what you are trying to do can be accomplished using
public static void main(String[] args) {
int[] intArray = {6, 4, 2, 3, 4, 6, 8};
int count = 0;
Map<Integer, Integer> repeatCounter = new HashMap<>();
for (int i : intArray) {
if (repeatCounter.containsKey(i)) {
int repeatedNTimes = repeatCounter.get(i);
repeatCounter.put(i, repeatedNTimes + 1);
}else{
repeatCounter.put(i, 1);
}
}
for (int i : intArray) {
if (repeatCounter.get(i) == 2) {
count++;
if (count == 2) {
System.out.println(i);
break;
}
}
}
}
Find 2nd/3rd or any duplicate with Set Interface
public static void main(String[] args){
int[] array = {3, 12, 9, 3, 8, 3, 12, 4, 7, 8};
int find_duplicate = 3;
// Add all duplicates to set
Set<Integer> storeDuplicates = new LinkedHashSet<>();
for (int i = 0; i < array.length-1; i++){
for (int j = i+1; j < array.length; j++){
if (array[i] == array[j]){
storeDuplicates.add(array[i]);
}
}
}
// Traverse set for find the 2nd/3rd/any other duplicate
int count = 0;
for (int i : storeDuplicates){
count++;
if (count == find_duplicate) System.out.println(find_duplicate+" duplicate is : "+i);
}
}

C language isMagicsquare function: What logic error is in my function code?

Although the output with the my current examples is correct, my code seems to have a logic error that doesn't give the right output for other cases.
I have previously addressed this issue:
"What would happen in your code if there are no repeated numbers, all numbers are used, your last column sum is correct, but your second-last column sum is incorrect?"
Any help would be very much appreciated.
My current code:
/* Magic Square */
#include <stdio.h>
#define MAX_N 100
#define TRUE 1
#define FALSE 0
int isMagicSquare(int square[MAX_N][MAX_N], int n);
int main(void) {
int square1[MAX_N][MAX_N] = {
{4, 9, 2},
{3, 5, 7},
{8, 1, 6}};
// should print TRUE
int check1 = isMagicSquare(square1, 3);
if (check1 == TRUE) {
printf("TRUE\n");
} else {
printf("FALSE\n");
}
int square2[MAX_N][MAX_N] = {
{20, 6, 7, 17},
{ 9, 15, 14, 12},
{13, 11, 10, 16},
{ 8, 18, 19, 5} };
/*{ 1 , 2 , 3 , 4 , },
{ 5 , 6 , 7 , 8 , },
{ 9 , 10, 11, 12, },
{ 13, 14, 15, 16,} };*/
/*{16, 2, 3, 13,},
{5, 11, 10, 8 ,},
{9, 7, 6, 12 ,},
{4, 14, 15, 1, } };*/
// should print FALSE
int check2 = isMagicSquare(square2, 4);
if (check2 == TRUE) {
printf("TRUE\n");
} else {
printf("FALSE\n");
}
int square3[MAX_N][MAX_N] = {
{17, 24, 1, 15, 8},
{23, 5, 7, 16, 14},
{ 4, 6, 13, 22, 20},
{10, 12, 19, 3, 21},
{11, 18, 25, 9, 2}};
// should print FALSE
int check3 = isMagicSquare(square3, 5);
if (check3 == TRUE) {
printf("TRUE\n");
} else {
printf("FALSE\n");
}
return 0;
}
int isMagicSquare(int square[MAX_N][MAX_N], int n) {
int row, col;
int drow, dcol;
int sum, sum1, sum2, sum3;
int boolean = 0;
//For Diagonals
sum = 0;
for (row = 0; row < n; row++) {
for (col = 0; col < n; col++) {
if (row == col)
sum += square[row][col];
}
}
for (row = 0; row < n; row++) {
sum3 = 0;
for (col = 0; col < n; col++) {
sum3 += square[n-row-1][col];
}
if (sum == sum3)
boolean = 1;
else {
return FALSE;
}
}
//For Rows
for (row = 0; row < n; row++) {
sum1 = 0;
for (col = 0; col < n; col++) {
sum1 = sum1 + square[row][col];
}
if (sum == sum1)
boolean = 1;
else {
return FALSE;
}
}
//For Columns
for (row = 0; row < n; row++) {
sum2 = 0;
for (col = 0; col < n; col++) {
sum2 = sum2 + square[col][row];
}
if (sum == sum2)
boolean = 1;
else {
return FALSE;
}
}
if (boolean == 1) {
//Check if All Numbers is used
for (row = 0; row < n; row++) {
for (col = 0; col < n; col++) {
if(square[row][col] > n*n || square[row][col] < 0) {
boolean = 0;
break;
}
}
}
//Check for Repeating Numbers
for (row = 0; row < n; row++) {
for(col = 0; col < n; col++) {
for(drow = row + 1; drow < n; drow++){
for(dcol = col + 1; dcol < n; dcol++) {
if(square[row][col] == square[drow][dcol]) {
boolean = 0;
break;
}
}
}
}
}
// if Statement End
}
else {
boolean = 0;
}
if (boolean == 1)
return TRUE;
else
return FALSE;
}
it seems the sum3 var is reinitialized to 0 inside the computation loop, plus the diagonal sum equality check should be outside of the loop.
I would rework it this way:
//For Diagonals
sum = 0;
sum3 = 0;
for (row = 0; row < n; row++) {
sum += square[row][row];
sum3 += square[row][n - 1 - row];
}
if (sum == sum3)
boolean = 1;
else {
return FALSE;
}
BTW I simplified a bit the diagonal calculation since it is linear, there is no need for 2 nested loops.
I found some other bugs in the final checks:
-0 should not be a valid value
-return instead of just breaking (break only exists the inner loop, the outer loop continues)
for (row = 0; row < n; row++) {
for (col = 0; col < n; col++) {
if(square[row][col] > n*n || square[row][col] <= 0) {
return FALSE;
}
}
}
//Check for Repeating Numbers
int storedNumbers[n * n];
for (row = 0; row < n; row++) {
for(col = 0; col < n; col++) {
storedNumbers[row + n * col] = square[row][col];
}
}
Then scan storedNumbers for duplicates.
Searching for duplicate values in an array
cheers
You can use a more compact scheme to check that all numbers 1 .. (n*n) are used with no duplicates using code such as:
int counts[n * n]; // Can't use an initializer with a VLA
memset(counts, '\0', sizeof(counts));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (square[i][j] <= 0 || square[i][j] > n * n)
return FALSE;
counts[square[i][j] - 1]++; // Map 1..n*n to 0..n*n-1
// if (++counts[square[i][j] - 1] > 1)
// return FALSE;
}
}
for (int i = 0; i < n * n; i++)
{
if (counts[i] != 1)
return FALSE;
}
Since there are n*n elements to check, this code uses space and time that is linear with respect to the number of elements in the magic square, which is within a constant factor of optimal.

Why is my program printing some of the arrays under 5 and over 15.5 and not all of them?

double[] prices = {20, 14.99, 3, 7.99, 2.34, 50, 1.49, 1.49, 8.99, 1.39, 50.00, 18.54, 12, 15.66, 6.77, 6.89, 13.67, 30, 40, 35.99};
double sum = 0;
int under5 = 0, over15= 0;
double[] under5list;
double[] over15list;
int c = 0;
//sum
for(int i = 0; i < prices.length; i++)
{
sum += prices[i];
}
System.out.println("Sum of prices: " + sum);
//under 5 over 15.5
for(int i = 0; i < prices.length; i++)
{
if(prices[i] < 5.0)
{
under5++;
}
else if(prices[i] > 15.5)
{
over15++;
}
}
under5list = new double[under5];
over15list = new double[over15];
for(int i = 0; i < prices.length; i++)
{
if(prices[i] < 5.0)
{
under5list[c] = prices[i];
c++;
}
if(prices[i] > 15.5)
{
c = 0;
over15list[c] = prices[i];
c++;
}
}
for(int i = 0; i < under5list.length; i++)
{
System.out.println("Under 5 prices:= " + under5list[i]);
}
for(int i = 0; i < over15list.length; i++)
{
System.out.println("Over 15.5 prices:= " + over15list[i]);
}
It looks to me like when c is set to 0 in the for loop, then next match for over or under will be placed at the beginning of the corresponding under-array or over-array.
You either need separate counters for indexing under5list and over15 list, or separate loops.

How can choose the five largest values in an array and put them in a new array?

How can choose the five largest values in an array and put them in a new array?
int[] a = { 1, 2, 5, 2, 4, 6, 8, 9, 1, 19 };
int[] largestValues = new int[5];
for (int i=0; i < 5; i++ ) {
System.out.println(largestValues[i]);
}
You can use following code
For Eg.
public static void main(String args[]) {
int i;
int large[] = new int[5];
int array[] = { 33, 55, 13, 46, 87, 42, 10, 34, 43, 56 };
int max = 0, index;
for (int j = 0; j < 5; j++) {
max = array[0];
index = 0;
for (i = 1; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
index = i;
}
}
large[j] = max;
array[index] = Integer.MIN_VALUE;
System.out.println("Largest " + j + " : " + large[j]);
}
}

flipped 2D array not filling properly

My 2D array will not fill properly I want the original array to stay unmodified, which it does, but then the next array to be flipped. For example 111, 222, 333 should be 333, 222, 111. If anyone could help it would be appreciated.
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int rows = 0, cols = 0;
System.out.println("Please enter the size or your rows first and then columns:");
Scanner input = new Scanner (System.in);
rows = input.nextInt();
cols = input.nextInt();
int [][] my2DArray = new int [rows][cols];
System.out.print("Now fill the array with the numbers you would like: ");
for(int i = 0; i < my2DArray.length; i++)
{
for(int j = 0; j < my2DArray[i].length; j++)
{
my2DArray[i][j] = input.nextInt();
}
System.out.println("");
}
System.out.println("Here is the origional unmodified array.");
for(int i = 0; i < my2DArray.length; i++)
{
for(int j = 0; j < my2DArray[i].length; j++)
{
System.out.print(my2DArray[i][j] + " ");
}
System.out.println("");
}
flipMy2DArray(my2DArray);
}
public static int[][] flipMy2DArray(int[][] inMy2DArray)
{
int [][] flipMy2DArray = new int [inMy2DArray.length][inMy2DArray.length];
for(int row = flipMy2DArray.length-1; row > 0; row--)
{
for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
{
flipMy2DArray[row][cols] = inMy2DArray[row][cols];
}
System.out.println("");
}
for(int row = 0; row < flipMy2DArray.length; row ++)
{
for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
{
System.out.print(flipMy2DArray[row][cols]+ " ");
}
System.out.println("");
}
return flipMy2DArray;
}
}

Resources