Java Program to find out duplicate number in an Array

Problem statement : In an array 1-100, many numbers are duplicates, how do you find it?

we can loop over array and put the values in a map to find out duplicate numbers with the number of time they repeat them selves.

            int[] numbers = new int [100];
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
       
        for(int i=0;i<100;i++){
            int temp = numbers[i];
            Integer count = map.get(temp);
            map.put(temp, (count == null) ? 1 : count + 1);
        }
       
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println(“Key : ” + entry.getKey() + ” Value repetition time: “
                + entry.getValue());
        }

The solution I proposed gives you the freedom of printing you data in several way or store the data in a manner which is most easy to retrieve. ie create a map for each number and save them against the key.

Advertisements

Code puzzle : find out median in a combined array

Problem Statement :

techChallenge

Solution I proposes :

image

public int getMedia(int[] input1, int[] input2) {
    int[] merged = new int[input1.length + input2.length];
    System.arraycopy(input1, 0, merged, 0, input1.length);
    System.arraycopy(input2, 0, merged, input1.length, input2.length);
    Arrays.sort(merged);
    int mergedSize = merged.length;
    int pos = mergedSize / 2;
    return mergedSize % 2 == 0 ? (merged[pos] + merged[pos + 1]) / 2 : merged[pos] / 2;
}

You can put your solution as comment Winking smile

Code – Springer Sun Spot Analyser Application

Hello Friends,

Today I was working on an interesting coding exercise in java. I am sharing a code in zipped format which is hosted on my web server. The program was written using TDD and can be executed end to end from main class as well as can be tested from test classes.

Problem 1 Code link : http://www.premaseem.com/shared_code/NasaSunAnalyzer1.zip

Problem  2 Code link : http://www.premaseem.com/shared_code/NasaSunAnalyzer.zip

The problem statement and description is as below :

Introduction
NASA wishes to analyse areas of the Sun for thermal activity. Their sensors provide raw data
which can visualised as a 5 by 5 grid of raw heat measurements that represents the Sun’s surface,
i.e.

image

 

Problem 1 :

The raw data for the analysis comprises of a list of integer measurements within the range from 0
(low heat) to 5 (high heat); therefore the raw data for the above grid would be as so:
5 3 1 2 0 4 1 1 3 2 2 3 2 4 3 0 2 3 3 2 1 0 2 4 3

This data needs to be analysed to find likely areas of increased solar activity. The analysis consists
of generating an Solar Activity Score for each location on the grid. The score is determined by
adding the location’s own raw heat value to its adjacent raw heat values. For instance, the score
of location (1,1) above would be as follows:
5 + 3 + 1 +
4 + 1 + 1 +
2 + 3 + 2
= 22

When dealing with locations around the edge of the grid the score should ignore values outside
the grid. For instance the score of location (0,0) is as follows:

5 + 3 +
4 + 1
= 13

—————————————————————————————————————-

Problem 2 :
Given that the raw data comprises of the following input:
5 3 1 2 0 4 1 1 3 2 2 3 2 4 3 0 2 3 3 2 1 0 2 4 3
Please provide a program that parses this input and outputs the scores for each grid location in
the same order. The expected result is:
13 15 11 9 7 18 22 20 18 14 12 18 22 23 17 8 15 23 26 19 3 8 14 17 12
You can use any mechanism you wish to provide input into your program but you must provide
evidence that the program’s output meets the requirements.

Problem 2
Another new requirement has now been added for your program. Rather than return a complete
list of all solar activity scores it is now important to return only the highest relevant scores.
To meet this need, an extra value will be added to the beginning of the input:
t n Grid
1 3 4 2 3 2 2 1 3 2 1
The required output of the analysis is to provide an ordered list of locations containing the t
number of highest rated locations and their respective scores.
Output should be a list of locations and their scores in the following form:
(x,y score:score)
i.e. the result that should be returned for the above input is:
(1,1 score:20)
Please provide proof that your updated program now fulfills this need for the following two test
cases:
Test 1
Input:
1 5 5 3 1 2 0 4 1 1 3 2 2 3 2 4 3 0 2 3 3 2 1 0 2 4 3
Expected output:
(3,3 score:26)
Test 2
Input:
3 4 2 3 2 1 4 4 2 0 3 4 1 1 2 3 4 4
Expected output:
(1,2 score:27)(1,1 score:25)(2,2 score: 23)

Code Puzzle : Is This String A Palindrome

Is This String A Palindrome

A palindrome is a word that is exactly the same whether read forwards or backwards. What you need to do is write a function that returns true if this string provided is a palindrome.

The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.

Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer.  It’s all about the participation!

 

Can a programmer work without the if statement ?

All the programming is nothing but usage of loops and conditional statements. Entire programming is mainly based on
1. If statements
2. For loops

If you know these you can easily do programming. A good programmer is one who uses these things efficiently to reduce execution time with better readability and maintainable code. Can a programmer work without the if (conditional statements) ?
It is difficult but entire programming can be done with only For loop construct.

How ? well inside the for loop there is conditional statement which could be used wisely to avoid using if.

for ( initialization; conditional statement; update){
do some thing
}

String sex = “male”;
if(sex.equalsIgnoreCase(“male”)) {
System.out.println(“Man is the king “);
} else{
System.out.println(“WoMan is the Queen “);
}

lets say we need to convert this if structure in for loop.
            for(;sex.equalsIgnoreCase(“male”);){
                System.out.println(“Man is the king “);
            }
In first attempt it will be some like this but this will cause the infinite loop so we must have to put break after our job is over to break the loop.
            for(;sex.equalsIgnoreCase(“male”);){
                System.out.println(“Man is the king “);
                break;
            }

Now how to use the else  part of the if structure.
for(;!sex.equalsIgnoreCase(“male”);){
System.out.println(“WoMan is the Queen “);
break;
}