Showing posts with label Coding test preparation. Show all posts
Showing posts with label Coding test preparation. Show all posts

Wednesday, February 2, 2022

Remove duplicate element from array

                // remove duplicate from an array using set

int[] ar = { 1, 1, 2, 3, 4, 5, 6, 6, 7, 8 };

Set<Integer> uniqueElements = new HashSet<>();

for (int i = 0; i < ar.length; i++) {

uniqueElements.add(ar[i]);

}

for (Integer i : uniqueElements) {

System.out.print(i+" ");

}

                  //Using HashMap

Map<Integer, Integer> uniqueElements = new HashMap<>();

for (int i = 0; i < ar.length; i++) {

if (uniqueElements.get(ar[i]) == null) {

System.out.print(ar[i] + " ");

uniqueElements.put(ar[i], ar[i]);

}

}


Monday, January 24, 2022

fibonacci series of first 10 element

                 int f1 = 0, f2 = 1;

int f3 = 0;

System.out.print(f1+" ");

System.out.print(f2+" ");

for (int i = 1; i <=8; i++) {

f3 = f1 + f2;

System.out.print(f3+" ");

f1 = f2;

f2 = f3;

}

Sunday, January 23, 2022

frequency of integer

 package arrayoperations;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class MainApp {

public static void main(String[] args) {

int[] a = { 1, 2, 2, 3, 4, 4, 4 };

Map<Integer, Integer> frequency = new HashMap();

for (int i = 0; i < a.length; i++) {

if (frequency.containsKey(a[i])) {

frequency.put(a[i], frequency.get(a[i]) + 1);

} else {

frequency.put(a[i], 1);

}

}

Set<Integer> keySet = frequency.keySet();

for(Integer i: keySet) {

System.out.println("item: "+ i+ " frequency: "+frequency.get(i));

}

}

}

Saturday, December 25, 2021

Singly Linked List operations

public class SinglyLinkedList {

       private ListNode head;

private static class ListNode {

private int data;

private ListNode next;

public ListNode(int data) {

this.data = data;

this.next = null;

}

}

// printing singly linked list

public void displayList(ListNode head) {

ListNode current = head;

while (current != null) {

System.out.println(current.data);

current = current.next;

}

}

// Reverse the linked list

public ListNode reverse(ListNode head) {

if (head == null) {

return head;

}

ListNode current = head;

ListNode previous = null;

ListNode next = null;

while (current != null) {

next = current.next;

current.next = previous;

previous = current;

current = next;

}

return previous;

}

public static void main(String[] args) {

// creating nodes

SinglyLinkedList sl = new SinglyLinkedList();

sl.head = new ListNode(10);

ListNode second = new ListNode(1);

ListNode third = new ListNode(2);

ListNode fourth = new ListNode(3);

// connecting nodes together

sl.head.next = second;

second.next = third;

third.next = fourth;

fourth.next = null;

sl.displayList(sl.head);

SinglyLinkedList.ListNode reverse = sl.reverse(sl.head);

sl.displayList(reverse);

}

}


Tuesday, December 21, 2021

String sorting using bubble sort

                String input = "geeksforgeeks";

char[] inputAr = input.toCharArray();

char temp;

for (int i = 0; i < inputAr.length ; i++) {// no of pass controller

for (int j = 0; j < inputAr.length -i- 1; j++) {

if (inputAr[j] > inputAr[j+1]) { //comparing with adjacent character.

temp = inputAr[j];

inputAr[j] = inputAr[j+1];

inputAr[j+1] = temp;

}

}

}

for (int i = 0; i < inputAr.length ; i++) {

System.out.println(inputAr[i]);

}

Sunday, August 22, 2021

Product sum using recursion.

Here we apply recursion because our array contains another array .so the process of addition is same as the outer array .

public class MainApp {

public static void main(String[] args) {

List<Object> a1 = new ArrayList<Object>();

a1.add(1);

a1.add(2);

a1.add(3);

a1.add(4);


List<Object> a2 = new ArrayList<Object>();

a2.add(5);

a2.add(6);

a2.add(7);

List<Object> arrays = new ArrayList<Object>();

arrays.addAll(a1);

arrays.add(a2);

arrays.add(8);

arrays.add(9);

int result = productSum(arrays, 1);

System.out.println(result);

}

public static int productSum(List<Object> inputs, int depth) {


int sum = 0;

for (Object obj : inputs) {

if (obj instanceof List) {

sum += productSum((List<Object>) obj, depth + 1);

} else {

sum += (int) obj;

}

}

return sum * depth;

}

}


Find frequency of character of a string using hashmap.

 public class MainApp {

public static void main(String[] args) {

String input = "aabbbcccdeefghijkkkkkk";

calculateFrequency(input);

}

private static void calculateFrequency(String input) {

Map<Character, Integer> frequency = new HashMap();

for (int i = 0; i < input.length(); i++) {

if (frequency.containsKey(input.charAt(i))) {

frequency.put(input.charAt(i), frequency.get(input.charAt(i)) + 1);

} else {

frequency.put(input.charAt(i), 1);

}

}

Set<Character> keySet = frequency.keySet();

for (Character c : keySet) {

System.out.println(c + ":" + frequency.get(c));

}

}

}


Testing controller

------Controller------------- @RestController @RequestMapping("/items") public class ItemController {     private final ItemServic...