import java.io.*;

public class duplicateArray
{
public static void main(String[] args)
{
// check the no. of args
if (args.length != 3)
{
System.out.println("No. of args is not correct");
System.exit(0);
}

// open file
BufferedReader in = openfile(args[0]);
if (in == null)
{
System.exit(0);
}

// read file
int Rows = stoi(args[1]);
int Cols = stoi(args[2]);
int[][] array = new int[Rows][Cols];
int r = 0;
for (String line; (line = readLine(in))!=null;)

{
String[] s = line.split(",");
for (int i=0; i<s.length; i++)
{
array[r] = stoi(s);
}
r = r + 1;
}

// find duplicate numbers
System.out.println("duplicated numbers:");
for (int i=0; i<Rows; i++)
{
for (int j=0; j<Cols; j++)
{
for (int n=i; n<Rows; n++)
{
for (int m=0; m<Cols; m++)
{
if (i*Cols+j < n*Cols+m)
{
if(array[j]==array[n][m])
System.out.print(array[j]+"… ");
}
}
}
}
}
}

static BufferedReader openfile(String filename)
{
try
{
return new BufferedReader(new FileReader(filename));
}
catch(FileNotFoundException e)
{
System.out.println("error: filename " + filename + " was not found");
return null;
}

}

static String readLine(BufferedReader in)
{
try
{
return in.readLine();
}
catch(IOException e)
{
System.out.println("error in read line");
return null;
}
}

static int stoi(String num)
{
try
{
return Integer.parseInt(num);
}
catch(NumberFormatException e)
{
System.out.println("number formatting error: " + num);
return 0;
}
}

}


I think I coded right for finding duplicates in an array, but I have no idea how I can find the number of duplicate and print it.

please help me out.

Edited 6 Years Ago by PuQimX: n/a

This article has been dead for over six months. Start a new discussion instead.