---
# System prepended metadata

title: Algos
tags: [gf]

---

# Algos


--------------------------------


--------------------------------
4
```
Remove duplicates **--MALACHITe** --Mawsitsit
Create a function called removeDuplicates that

takes an array of integers (array) as an input,
returns a result array / list where all the elements are unique
Example cases:

removeDuplicates([1, 1, 2, 2, 3]);
Should return [1, 2, 3]

removeDuplicates([1, 2, 3]);
Should return [1, 2, 3]

removeDuplicates([]);
Should return []
```

--------------------------------
5
```
Difference **--Malachite**
Create a function called difference that

takes 2 arrays of integers (array1) and (array2) as an input,
returns the difference of the 2 arrays(Those elements that are present array1 but not present in array2).
Example cases:

difference([1, 2, 3], [2, 3, 4]);
Should return [1]

difference([1, 2], [3, 4]);
Should return [1, 2]

difference([], []);
Should return []
```


--------------------------------

--------------------------------

--------------------------------


--------------------------------
9
Find the missing letter: 
Create a function called `findMissingLetter` that takes in an array of consecutive letters and finds the missing letter and returns it.
 
 Example input/output: 
``findMissingLetter(new char[]{'a','b','c','d','f'})`` 

returns ``'e'``

--------------------------------
10 
Array.diff: 
Create a function called `arrayDiff` that takes in two arrays, removes all elements from the first array that are also present in the second array and returns the remaining elements in the same order.

Example input/output: 
`arrayDiff(new int[]{1,2}, new int[]{1})`
returns ```[2]```

--------------------------------
11

Directions Reduction: 
Given an array of directions, simplify the directions by removing any opposite directions that cancel each other out (e.g. "NORTH" and "SOUTH"). Example input/output: 
`dirReduc(new String[]{"NORTH","SOUTH","SOUTH","EAST","WEST","NORTH","WEST"})` 
returns ``{"WEST"}.``

--------------------------------
12
Counting Duplicates: 
Given a string, count how many characters appear more than once in the string (case-insensitive). Example input/output: ``duplicateCount("aabBcde")``
returns `2`

--------------------------------
13

Format a string of names like 'Bart, Lisa & Maggie':  Given an array of names, format the names as a string with commas and "and" appropriately placed. Example input/output: 
`formatNames(new String[]{"Bart","Lisa","Maggie"})` 
returns ``"Bart, Lisa & Maggie"``

--------------------------------
14
String incrementer: Given a string with a number at the end, increment the number by 1 while preserving the original format of the string. Example input/output: `incrementString("foo0042") `
returns ``"foo0043"``


15

Find the odd int
Given an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times.
Example input/output:
`findOdd(new int[]{1, 1, 2, 2, 3, 3, 3})`
returns `3`

`findOdd(new int[]{4, 4, 3, 3, 2})`
returns `2`




17

Create phone number
Given an array of 10 integers, format them into a phone number in the format of (123) 456-7890.
Example input/output:
`createPhoneNumber(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0})`
returns `"(123) 456-7890"`

`createPhoneNumber(new int[]{4, 5, 6, 7, 8, 9, 0, 1, 2, 3})`
returns `"(456) 789-0123"`

18

Find the next perfect square
Given a positive integer n, find the next smallest integer (>=n) which is a perfect square.
Example input/output:
`nextSquare(9)`
returns `16`

`nextSquare(15)`
returns `-1`

¬
20

Longest common prefix
Given an array of strings, find the longest common prefix among them.
Example input/output:


`longestCommonPrefix(new String[]{"flower", "flow", "flight"}) `
returns ``"fl"``

`longestCommonPrefix(new String[]{"dog", "racecar", "car"}) `
returns ``""``

21

Caesar cipher
Given a string and a shift number, encrypt the string using a Caesar cipher. Each letter should be shifted by the specified number (wrapping around from 'z' to 'a' as needed).
Example input/output:


`caesarCipher("hello", 3) `
returns ``"khoor"``

`caesarCipher("abc xyz", 5) `
returns ``"fgh cde"``

22

Title Case
Given a string and a list of words to exclude, capitalize the first letter of each word in the string except for the excluded words.
Example input/output:

`titleCase("the quick brown fox", new String[]{"the", "brown"}) `
returns ``"the Quick brown Fox"``
`titleCase("hello world", new String[]{"world"}) `
returns ``"Hello world"``

23

Valid Parentheses
Given a string containing only parentheses, determine if the parentheses are valid (i.e., each opening parenthesis has a matching closing parenthesis).
Example input/output:


``isValidParentheses("()[]{}") ``
returns `true`

``isValidParentheses("([)]") ``
returns `false`

24

Matrix transpose
Description: Given a matrix, transpose the matrix by flipping its rows and columns.
Example input/output:
`transposeMatrix(new int[][]{{1,2,3},{4,5,6},{7,8,9}}) `
returns` {{1,4,7},{2,5,8},{3,6,9}}`

`transposeMatrix(new int[][]{{1,2},{3,4}})` 
returns ``{{1,3},{2,4}}``

25
Matrix multiplication
Description: Given two matrices, multiply them together to produce a third matrix.
Example input/output:

`multiplyMatrices(new int[][]{{1,2,3},{4,5,6}}, new int[][]{{7,8},{9,10},{11,12}})` 
returns ``{{58,64},{139,154}}``

`multiplyMatrices(new int[][]{{1,2},{3,4}}, new int[][]{{5,6},{7,8}})` 
returns ``{{19,22},{43,50}}``


26

```
Reverse a string
Given a string, return a new string with the characters reversed.
Example input/output:
reverseString("hello world")
returns "dlrow olleh"

reverseString("abcdefg")
returns "gfedcba"
```

27
```
Remove all occurrences of a character from a string
Given a string and a character, remove all occurrences of that character from the string.
Example input/output:
removeCharacter("hello world", 'l')
returns "heo word"

removeCharacter("hello world", 'z')
returns "hello world"

```

28
```
Find the second largest number in an array
Given an array of integers, find the second largest number in the array.
Example input/output:
secondLargest(new int[]{1,2,3,4,5})
returns 4

secondLargest(new int[]{1,1,1,1,1})
returns -1 (or some other indication that there is no second largest number)
```
29
```

```
30
```

```
31
```
Find the maximum  of two integers in an array
Given an array of integers, find the maximum product that can be obtained by multiplying any two integers in the array.
Example input/output:
maxProduct(new int[]{1,2,3,4,5})
returns 20

maxProduct(new int[]{-1,-2,-3,-4,-5})
returns 20
```
32
```
Remove all vowels from a string

Given a string, remove all vowels from it.

Example input/output:
removeVowels("hello world")
returns "hll wrld"

removeVowels("aeiou")
returns ""
```
33
```
Find the intersection of two arrays
Given two arrays, return a new array that contains only the elements that are present in both arrays.
Example input/output:
intersection(new int[]{1,2,3,4}, new int[]{3,4,5,6})
returns [3,4]

intersection(new int[]{1,2,3}, new int[]{4,5,6})
returns []
```
34
```
Determine if two strings are rotations of each other
Given two strings, determine if one is a rotation of the other (i.e., the characters are in the same order, but shifted by some number of positions).
Example input/output:
isRotation("abcde", "cdeab")
returns true

isRotation("hello", "world")
returns false
```

