Created
September 22, 2023 09:12
-
-
Save kenduigraha/3b7cfb491f2664d95881aac23b469288 to your computer and use it in GitHub Desktop.
scanArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given an array of random characters with size n, | |
// String[] stringArr = ["A", "C", "K", "B" .....]; | |
// write a procedure that will scan the array from the first element | |
// and print the first character that appear three times during scanning | |
const TOTAL_APPEAR = 3; | |
function scanArray(data) { | |
let flag = 0; | |
const result = [] | |
const dataLength = data.length; | |
// default | |
if (data.length === 0) return 'data is empty'; | |
for (let i = 0 ; i < dataLength; i++) { | |
let count = 0; | |
for (let j = i; j < dataLength; j++) { | |
if (data[i] === data[j]) { | |
count++; | |
} | |
} | |
if (count === TOTAL_APPEAR) { | |
flag = 1; | |
result.push(data[i]); | |
} | |
} | |
if (flag > 0) return result.toString(); | |
// default | |
if (flag === 0) return 'not found'; | |
} | |
// data is empty | |
// const stringArr = []; | |
// A | |
// const stringArr = ["A","A", "A"]; | |
// B | |
// const stringArr = ["A","A", "A","B", "B", "B"]; | |
// not found | |
// const stringArr = ["A", "C", "K", "B", "BA", "B", "AC", "C", "A"]; | |
// A,B | |
// const stringArr = ["A", "C", "K", "B", "B", "B", "A", "C", "A"]; | |
// BB | |
// const stringArr = ["C", "K", "BB", "BB", "BB", "A", "C", "A"]; | |
// A,B, Z | |
const stringArr = ["A", "C", "K", "B", "Z", "B", "B", "A", "Z", "C", "A", "Z"]; | |
console.log(scanArray(stringArr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment