How can I check multiple duplicates in one array? [ReadyJava] - java

This program receives 100 inputs and then outputs the lowest value. I need help checking all duplicated values inputted.
Example with 5 inputs:
5,1,1,5,4
The smallest value: 1
The amount of duplicated values: 4
import java.awt.*;
import hsa.Console;
public class ArrayNumbers
{
static Console c;
public static void main (String[] args)
{
c = new Console ();
int number[] = new int [100], i = 1, output = 0;
c.print ("Enter number #1:");
number [0] = c.readInt ();
output = number [0];
for (int count = 0 ; count < 99 ; count++)
{
c.print ("Enter number #" + (count + 2)+ ":");
number [i] = c.readInt ();
if (number [i] < output)
{
output = number [i];
}
i++;
}
c.print(output);
} // main method
} // ArrayNumbers class

It can be done by using map. Please find below code for the same:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class MinAndDuplicates {
public static void main(String[] args) {
int number, totalDupCount=0;
Scanner stdin = new Scanner(System.in);
Map<Integer, Integer> map = new HashMap<Integer,Integer>();
//Prepare map with duplicate count for each number
for (int i = 0; i < 5; i++) {
number=stdin.nextInt();
if(map.containsKey(number)){
map.put(number, ((Integer)map.get(number)+1));
}else{
map.put(number, 1);
}
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int numberCount= entry.getValue();
if (numberCount> 1) {
totalDupCount+= numberCount;
}
}
System.out.println(totalDupCount);
}
}
Hope this helps!

Below is the logic to perform your duplicate count in O(n) time and O(1) extra space.
try this
// Function to find counts of all elements present in
// arr[0..n-1]. The array elements must be range from
// 1 to n
// Traverse all array elements
int[] arr = {5,1,1,5,4};
int i = 0,n=arr.length;
int totalDupCount = 0;
while (i < n) {
// If this element is already processed,
// then nothing to do
if (arr[i] <= 0) {
i++;
continue;
}
// Find index corresponding to this element
int elementIndex = arr[i] - 1;
// If the elementIndex has an element that is not
// processed yet, then first store that element
// to arr[i] so that we don't loose anything.
if (arr[elementIndex] > 0) {
arr[i] = arr[elementIndex];
// After storing arr[elementIndex], change it
// to store initial count of 'arr[i]'
arr[elementIndex] = -1;
} else {
// If this is NOT first occurrence of arr[i],
// then increment its count.
arr[elementIndex]--;
// And initialize arr[i] as 0 means the element
// 'i+1' is not seen so far
arr[i] = 0;
i++;
}
}
System.out.println("Below are counts of all elements");
for (int j = 0; j < n; j++){
if(Math.abs(arr[j]) >= 2){
System.out.println(j + 1 + "->" + Math.abs(arr[j]));
totalDupCount +=Math.abs(arr[j]);
}
}
System.out.println("Total Duplicate Count in Array is : "+totalDupCount);
}
Output
Below are counts of all elements
1->2
5->2
Total Duplicate Count in Array is : 4

(This post is slightly uglier than my first approach, but it solves the poster's question the way they'd like it to. I have decided to put it as a separate answer.)
If you want to count the number of duplicated numbers multiple times
depending on how often they show up, you can do it with a
HashMap.
The Approach
Create a HashMap to count how often each number appears.
When reading input, increment the occurrences of that number.
To find the number of "duplicates", iterate through the HashMap and sum all of the occurrences which happen more than once.
Example
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
public class DuplicateCounter {
static int[] numbers = new int[100];
static HashMap<Integer, Integer> occurrences = new HashMap<>();
public static void main(String[] args) {
readInput();
int duplicates = countDuplicates();
System.out.printf("%d numbers appeared multiple times.\n", duplicates);
}
public static void readInput() {
Scanner stdin = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
int number = stdin.nextInt();
numbers[i] = number;
incrementCount(number);
}
stdin.close();
}
public static int countDuplicates() {
int duplicates = 0;
for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) {
int numOfOccurrences = entry.getValue();
if (numOfOccurrences > 1) {
duplicates += numOfOccurrences;
}
}
return duplicates;
}
public static void incrementCount(int number) {
if (occurrences.get(number) != null) {
int previous = occurrences.get(number);
occurrences.put(number, previous + 1);
} else {
occurrences.put(number, 1);
}
}
}

Related

median and mode displaying -1

Hey guys I have been learning coding recently and got an assignment of finding the mean, median and mode of a number of integers in an integer array. The issue that I am facing is that my median and mode displays -1 and I'm not too sure how to fix it, my "if user inputs no, print an error statement" is also not working and I would be very grateful if someone could help me out.
This is my code:
package com.company;
import java.io.File;
import java.io.FileNotFoundException;
import static com.company.ProjectConstants.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] a = new int[MAXDATA];
int counter = 0;
boolean fileDone = false;
boolean inputOk;
String userInput;
String theDataFile;
Scanner s = new Scanner(System.in);
genProgInfo();
userInput = s.nextLine();
userInput = userInput.toLowerCase();
while (!userInput.equals("yes") && (!userInput.equals("no"))) {
System.out.println("ERROR: Please input either yes or no: ");
userInput = s.nextLine();
userInput = userInput.toLowerCase();
}
inputOk = userInput.equals("yes");
initDataStorage(a);
//do {
try {
// create file & scanner objects
System.out.println("enter one of the file names:\nData10File.txt\nData30file.txt\nData35file.txt");
theDataFile = s.next();
theDataFile = theDataFile.toLowerCase();
//fName = userInput;
File f = new File(theDataFile);
Scanner sc = new Scanner(f);
// store file data in array, a
for (int i = 0; i < MAXDATA; i++) {
if (sc.hasNext()) {
a[i] = sc.nextInt();
} else {
fileDone = true;
sc.close();
break;
}
}
// print error message if file data exceeds the range of array
if (!fileDone) {
System.out.println("\n\tCAUTION: file has additional data, consider making array larger.");
}
} catch (FileNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
//} while (inputOk);
s.close();
for (int i=0; i<MAXDATA; i++) {
if (a[i] != -1) {
counter = a[i];
}
}
System.out.println("counter: "+ counter);
displayResults(calcMean(a), calcMedian(a), calcMode(a));
}
public static void initDataStorage(int[] data) {
for (int i = 0; i < MAXDATA; i++) {
data[i] = INVALID;
}
}
public static double calcMean(int[] data) {
int counter = 0;
int mean;
int sum = 0;
for (int i = 0; i < MAXDATA; i++) {
if (data[i] != -1) {
sum += data[i];
counter++;
}
}
mean = sum / counter;
return mean;
}
public static double calcMedian(int[] data) {
int middle = data.length / 2;
if (data.length % 2 == 1) {
return data[middle];
} else {
return (data[middle -1] + data[middle]) / 2.0;
}
}
public static int calcMode(int[] data) {
int mode = 0, maxCount = 0;
for (int i = 0; i < data.length; ++i) {
int count = 0;
for (int j = 0; j < data.length; ++j) {
if (data[j] == data[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mode = data[i];
}
}
return mode;
}
public static void genProgInfo() {
System.out.println("This program will calculate the mean, median, and mode of a number of integers stored in the array");
System.out.println("Would you like to continue?");
}
public static void displayResults(double mean, double median, int mode) {
System.out.println("mean: " + mean);
System.out.println("median: " + median);
System.out.println("mode: " + mode);
}
}
I'm assuming the value of INVALID is -1 based on the other code I see and your description of what it outputs. Your array is of length MAXDATA and is initially filled with value INVALID in all elements. You then fill it with n values where n may be less than MAXDATA, and in that (probably common) case, many or even most of the values in the array are the INVALID value.
Your calcMean function is correctly skipping over the -1 (INVALID?) values and not including them in the calculation. Note however that the valid values are all at the beginning of the array and once you find an invalid value, you could break out of the loop in calcMean.
But the calcMedian and calcMode functions are not accounting for the invalid values. If n is significantly less than MAXDATA, then -1 probably really is the mode. Your calcMedian function has an additional problem as well, in that the (valid) data needs to be sorted in order for the "middle" or median value to be in the middle of the array.
Bonus question for your assignment: What if -1 occurs in the input file?

Array out of bound Java

I am working on a small program for school where i need to input an array. Every element in the array must be checked if the neighbours are smaller than the Element. If the neighbours are smaller --> This elements get an extra *
But i got problems with the boundaries, for example the first one and the last one. The first one has only one neighbour.
public class Tops {
Scanner sc = new Scanner(System.in);
public void calculate(){
String number;
ArrayList<String> numbers; //def’s, decl’s ArrayList
ArrayList<String> after;
numbers = new ArrayList<>(); //creates numbers
after = new ArrayList<>();
number = sc.next();
while (!"0".equals(number)) {
numbers.add(number); //initializes each ArrayList element
number = sc.next();
}
for (int i = 0; i < numbers.size(); i++) {
if (Integer.parseInt(numbers.get(i)) > Integer.parseInt(numbers.get(i+1)) && Integer.parseInt(numbers.get(i)) > Integer.parseInt(numbers.get(i-1)) ){
String replace = numbers.get(i)+"*";
after.add(replace);
} else {
after.add(numbers.get(i));
}
}
for(int i=0;i<after.size();i++){
System.out.print(after.get(i)+ " ");
}
}
public static void main(String[] args) {
new Tops().calculate();
}
}
i hope you can help me
Since you need both neighbours of an element to be smaller to add an "*", you can skip the first and the last elements (because they are missing one of the neighbours).
Adapting your code with this consideration:
import java.util.ArrayList;
import java.util.Scanner;
public class Tops {
private static final Scanner INPUT_SCANNER = new Scanner(System.in);
public static void calculate() {
// Initialize numbers
ArrayList<String> numbers = new ArrayList<>();
String number = INPUT_SCANNER.next();
while (!"0".equals(number)) {
numbers.add(number);
number = INPUT_SCANNER.next();
}
// Check the neighbours
ArrayList<String> after = new ArrayList<>();
// Check first element
if (numbers.get(0) > numbers.get(1)) {
after.add(numbers.get(0) + "*");
} else {
after.add(numbers.get(0));
}
// Check other elements
for (int i = 1; i < numbers.size() - 1; i++) {
int previous = Integer.parseInt(numbers.get(i - 1));
int current = Integer.parseInt(numbers.get(i));
int next = Integer.parseInt(numbers.get(i + 1));
String newElement = numbers.get(i);
if (current > next && current > previous) {
newElement = newElement + "*";
}
after.add(newElement);
}
// Check last element
if (numbers.get(numbers.size() - 1) > numbers.get(numbers.size() - 2)) {
after.add(numbers.get(numbers.size() - 1) + "*");
} else {
after.add(numbers.get(numbers.size() - 1));
}
after.add(numbers.get(numbers.size() - 1));
// Show the result
for (int i = 0; i < after.size(); i++) {
System.out.print(after.get(i) + " ");
}
}
public static void main(String[] args) {
calculate();
}
}
Execution example:
INPUT: 1 2 3 2 1 0
OUTPUT: 1 2 3* 2 1
Notice that:
I have made your calculate method static since you do not have any non-static field on the Tops class
I have moved the numbers initialization to the begining of the method
The final numbers are added on the after list only once so you do not need to initialize it
To perform the comparison without converting the current number from string to int twice per loop iteration, I have moved the int formats out of the if comparison.

how to check 1 is prime number in java and Hashmap's output is wrong for String type

I am currently solving this problem on Hackerrank, and below is the code that I have written so far.
When I ran it, there are a couple of errors: the output is totally wrong even though the input and the process are correct, and when the input is 1, it does not add it into my hashmap. I believe that both errors exist because of using HashMap. For example, if the input is
3(testcases) ==> 12, 5, 7. It will print out Prime, Prime, Not prime instead of the correct output like Not prime, Prime, Prime.
For more information, I commented out the lines,saying "output purposes". You can see, it added to the hashmap in correct order, but when it prints, it messed up.
So, I am just curious if someone can please explain why printing is wrong, and fix for the part when the input is 1.
import java.io.*;
import java.util.*;
public class Solution1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] myarray = new int[n]; // read number of testcases
for(int i=0;i<n;i++){ // add input to my int array
myarray[i]=sc.nextInt();
}
HashMap<Integer, String> newmap = new HashMap <>(); // create empty hashmap
int temp;
int value;
for(int i=0;i<n;i++){ // loop based on num of testcases
temp =myarray[i];
boolean isprime = true;
if(temp ==1){ // hardcode for input 1
isprime = false;
continue;
}
for(int j=2;j<=temp/2;j++){ // checking whether the input is prime or not
value = temp%j;
if(value==0){
isprime = false;
continue;
}
}
if(isprime==true){
//System.out.println("temp(Prime): "+temp); //output purpose
newmap.put(temp,"Prime");
}
else{
//System.out.println("temp(Not prime): "+temp); //output purpose
newmap.put(temp,"Not prime");
}
}
Set set = newmap.entrySet();
Iterator iterator = set.iterator();
//printing out values of the each element in hashmap(newmap)
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.println(mentry.getValue());
}
}
}
The problem in your code is that your myarray is not sorted and and when you are inserting into newmap , it's not getting inserted in the desired order.
I recommend you to sort the array and used ordered map like LinkedHashMap or linked list.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] myarray = new int[n]; // read number of testcases
Arrays.sort(myarray);
for (int i = 0; i < n; i++) { // add input to my int array
myarray[i] = sc.nextInt();
}
HashMap<Integer, String> newmap = new LinkedHashMap<Integer, String>(); // create
// empty
// hashmap
int temp;
int value;
for (int i = 0; i < n; i++) { // loop based on num of testcases
temp = myarray[i];
boolean isprime = true;
if (temp == 1) { // hardcode for input 1
isprime = false;
break;
}
for (int j = 2; j <= temp / 2; j++) { // checking whether the input
// is prime or not
value = temp % j;
if (value == 0) {
isprime = false;
break;
}
}
if (isprime == true) {
// System.out.println("temp(Prime): "+temp); //output purpose
newmap.put(temp, "Prime");
} else {
// System.out.println("temp(Not prime): "+temp); //output
// purpose
newmap.put(temp, "Not prime");
}
}
Set set = newmap.entrySet();
Iterator iterator = set.iterator();
// printing out values of the each element in hashmap(newmap)
while (iterator.hasNext()) {
Map.Entry mentry = (Map.Entry) iterator.next();
System.out.println(mentry.getValue());
}
}
}
#Jim Lewis and #Michael Markidis have already provided the right answers for how to fix the bugs in this code.
I think to satisfy the requirements of the coding exercise you're doing, you'll need to output the results in the same order as the input numbers, so you'll want to collect the results into something with an order, rather than the (unordered) HashMap. Here's some slightly cleaned up code that uses an ArrayList instead (though frankly you should be able to just print out the results as you go):
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] inputs = new int[n]; // read number of testcases
for (int i = 0; i < n; i++) { // add input to my int array
inputs[i] = sc.nextInt();
}
List<String> results = new ArrayList<>();
for (int number : inputs) {
boolean isprime = true;
if (number == 1) { // hardcode for input 1
isprime = false;
} else {
for (int j = 2; j <= number / 2; j++) { // checking whether the input is prime or not
if (number % j == 0) {
isprime = false;
continue;
}
}
}
results.add(isprime ? "Prime" : "Not prime");
}
for (String result : results) {
System.out.println(result);
}
}
}
A HashMap doesn't have predictable order. However, you can print out the keys associated with each value like this:
for (Iterator<Integer> iter = newmap.keySet().iterator(); iter.hasNext(); )
{
int key = iter.next();
System.out.println(key + "=" + newmap.get(key));
}
This will output:
5=Prime
7=Prime
12=Not prime
EDIT: As suggested by MeBigFatGuy and smarx, an entrySet() iterator is better while used with an enhanced loop.
Example:
for (Map.Entry mentry : newmap.entrySet())
{
System.out.format("%s = %s\n", mentry.getKey(), mentry.getValue());
}
You are assuming that the Iterator over the entrySet of your Hashmap will return the entries in the same order you added them. A plain old HashMap guarantees nothing of the sort (no pun intended). Try using a LinkedHashMap instead.
While checking for 1 , update in map before continue;
Inside Inner for loop ,
Use break instead of continue
Some tweaks
- If your array doesn't have duplicates then dont waste memory in
hashmaps. Loop on array.
coding exercises has large input to be read , use bufferedreader instead for faster throughput
use generics properly

pairs of numbers that have a difference of K

This was my online interview question, and I already submitted the answer. However, the compilation was terminated due to time so I just submitted. Could I get feedback from you? Thanks in advance.
Problem:
Given N numbers , [N<=10^5] we need to count the total pairs of numbers that have a difference of K
Input Format:
1st line contains N & K (integers).
2nd line contains N numbers of the set. All the N numbers are assured to be distinct.
Output Format:
One integer saying the no of pairs of numbers that have a diff K.
Sample Input #00:
5 2
1 5 3 4 2
Sample Output #00:
3
My code:
import java.io.*
import java.util.*;
public class DiffNumbers {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line1 = in.readLine();
String line2 = in.readLine();
int n = Integer.parseInt(line1.split(" ")[0]);
int diff = Integer.parseInt(line1.split(" ")[1]);
Hashtable table = new Hashtable();
int[] arr = new int[n];
for(in i=0; i<n; i++) {
arr[i] = Integer.parseInt(line2.split(" ")[i]);
table.put(Integer.parseInt(line2.split(" ")[i]));
}
int count = 0;
for(in i=0; i<n; i++) {
if(table.containsKey(arr[i]+diff) {
count++;
}
}
system.out.println(count);
}
}
Using HashMap/Table needs extra space. If you want to avoid it you can do it this way
1) Sort the array
2) initialize outputCount as 0
3) Let there be two pointers. "first" start with 0 and "Other" pointer start with 1.
4)
while(arr[other]-arr[first] <requiredDifference)
other ++;
if(arr[other]-arr[first] == requiredDifference)
outputCount++;
else // no match for arr[first]
first++;
5)
return outputCount;
explanation :-
When difference is more than requiredDifference you stop moving ahead "other" poiner. So there is no match for arr[first]. So move ahead first counter. Now do the same logic for new arr[first]. This time you will continue checking from current position of "other" as array is sorted; lower number will not have required match.
public static void main(String[] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line1 = in.readLine();
String line2 = in.readLine();
int diff = Integer.parseInt(line1.split(" ")[1]);
Map<Integer, Object> nMap = new HashMap<Integer, Object>();
Map<Integer, Boolean> uMap = new HashMap<Integer, Boolean>();
Map<Integer, Boolean> lMap = new HashMap<Integer, Boolean>();
String[] numbers = line2.split(" ");
//init maps
for(String number : numbers){
Integer intValue = Integer.valueOf(number);
nMap.put(intValue, new Object()); //original set, N
uMap.put(intValue + diff, false); //upper-bound map
lMap.put(intValue - diff, false); //lower-bound map
}
int count = 0;
for(Integer nKey : nMap.keySet()){
//Do if the lower-bound of n exists in N and if n hasn't used as an upper-bound?
if(nMap.get(nKey - diff) != null && !uMap.get(nKey)){
count++;
//Mark the lower-bound as used.
lMap.put(nKey - diff, true);
}
//Do if the upper-bound of n exists in N and if n hasn't used as an lower-bound?
if(nMap.get(nKey + diff) != null && !lMap.get(nKey)){
count++;
//Mark the upper-bound as used.
uMap.put(nKey + diff, true);
}
}
System.out.println(count);
}
There's not much reason to store the integers in both an array and a hashtable. We can modify your code to do the all of the work in a single for loop.
for(int i=0; i<n; i++) {
int j = Integer.parseInt(line2.split(" ")[i]) //probably not how I would go about this
table.put(j);
if(table.containsKey(j+diff)) {
count++;
}
if(table.containsKey(j-diff)) {
count++;
}
}

Returning the element number of the longest string in an array

I'm trying to get the longest method to take the user-inputted array of strings, then return the element number of the longest string in that array. I got it to the point where I was able to return the number of chars in the longest string, but I don't believe that will work for what I need. My problem is that I keep getting incompatible type errors when trying to figure this out. I don't understand the whole data type thing with strings yet. It's confusing me how I go about return a number of the array yet the array is of strings. The main method is fine, I got stuck on the ???? part.
public static void main(String [] args)
{
Scanner inp = new Scanner( System.in );
String [] responseArr= new String[4];
for (int i=0; i<4; i++)
{
System.out.println("Enter string "+(i+1));
responseArr[i] = inp.nextLine();
}
int highest=longestS(responseArr);
}
public static int longestS(String[] values)
{
int largest=0
for( int i = 1; i < values.length; i++ )
{
if ( ????? )
}
return largest;
}
for (int i = 0; i < values.length; i++)
{
if (values[i].length() > largest)
{
largest = values[i].length();
index = i;
}
}
return index;
Note: initialize the int i with 0 - array index is 0-based.
Back in your main, you could then do System.out.println("Longest: " + responseArr[highest]); etc.
Here's how I'd write it:
public static int findIndexOfLongestString(String[] values)
{
int index = -1;
if ((values != null) && (values.length > 0))
{
index = 0;
String longest = values[0];
for (int i = 1; i < values.length; ++i)
{
if (values[i].length() > longest.length())
{
longest = values[i];
index = i;
}
}
}
return index;
}
You will want to store two things in your longestS method: the largest length so far, and the array index of the largest length. Also keep in mind that array indices start at 0 in Java. A for loop initialised with int i = 1 is actually going to start at the second index.
My solution:
public class JavaApplication3
{
public static void main(String[] args)
{
String[] big={"one","two","three"};
String bigstring=null;
int maxlength=0;
for(String max:big)
{
if(maxlength<max.length())
{
maxlength=max.length();
bigstring=max;
}
}
System.out.println(bigstring);
}
}

Categories