Sponsored Links
Ad by Google
While preparing for an interview, most of the candidates googled,the popular java interview question and start remembering those popular question's answer only. But nowadays only theory is not enough, if you are planing to change your job or looking for a better opportunity, than you must have to be not only technically but logically sound also.
Nowadays, in any interview a programming question must asked, it doesn't matter if it's for Google,Amazon,Facebook JP Morgan or it's a mid level organization. Of course questions are not very tough. It's simple java program to solve any puzzle or finding the best way to achieve a solution based on time and space complexity, simple dead lock situation, implementing stack in java, java memory model etc.
So I am planing to share with you simple java programing interview questions, like previously I already shared with you one of the very popular java programing interview question "Remove duplicate characters from a string with o(n) time complexity" here. In this post going to show you a simple java program to print repeated numbers in an array.
ArraysUtil.java
OUT PUT:
duplicate number = 1
duplicate number = 1
duplicate number = 2
Nowadays, in any interview a programming question must asked, it doesn't matter if it's for Google,Amazon,Facebook JP Morgan or it's a mid level organization. Of course questions are not very tough. It's simple java program to solve any puzzle or finding the best way to achieve a solution based on time and space complexity, simple dead lock situation, implementing stack in java, java memory model etc.
So I am planing to share with you simple java programing interview questions, like previously I already shared with you one of the very popular java programing interview question "Remove duplicate characters from a string with o(n) time complexity" here. In this post going to show you a simple java program to print repeated numbers in an array.
ArraysUtil.java
package com.javamakeuse.poc; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArraysUtil { public static int[] returnDuplicates(int[] numArray) { // sorting an array Arrays.sort(numArray); List<Integer> duplicatElements = new ArrayList<>(); for (int i = 1; i < numArray.length; i++) { if (numArray[i] == numArray[i - 1]) { duplicatElements.add(numArray[i]); } } // return primitive array return getPrimitiveArray(duplicatElements); } // return array of primitive types public static int[] getPrimitiveArray(List<Integer> numberLsit) { int[] priArray = new int[numberLsit.size()]; for (int i = 0; i < numberLsit.size(); i++) { priArray[i] = numberLsit.get(i); } return priArray; } public static void main(String[] args) { int[] numArray = { 1, 2, 3, 1, 4, 5, 1, 6, 2 }; int[] dups = returnDuplicates(numArray); for (int i = 0; i < dups.length; i++) { System.out.println("duplicate number = " +dups[i]); } } }
OUT PUT:
duplicate number = 1
duplicate number = 1
duplicate number = 2
Sponsored Links
0 comments:
Post a Comment