Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Counting Duplicates

function duplicateCount(text){

    let count = 0
    let object ={}

   for(let i of text){
     i = i.toLowerCase()
       if(!object[i]){
        object[i] =1
       }
       else{
           object[i]++
       }
   }
   console.log(object)
   for( let i in object){
    if(object[i] >1){
        count ++
    }
}
   return count
}
Comment

Counting Duplicates

// Count the number of Duplicates
// Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

/*
  Example
  "abcde" -> 0 # no characters repeats more than once
  "aabbcde" -> 2 # 'a' and 'b'
  "aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
  "indivisibility" -> 1 # 'i' occurs six times
  "Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
  "aA11" -> 2 # 'a' and '1'
  "ABBA" -> 2 # 'A' and 'B' each occur twice
*/

function duplicateCount(text){
  let duplicateArr = {}, splitText = text.toLowerCase().split('')
  
  splitText.forEach(letter => {
    if(duplicateArr.hasOwnProperty(letter)) duplicateArr[letter] += 1
    else duplicateArr[letter] = 0
  })
  
  const duplicateCount = Object.values(duplicateArr).toString().match(/[1-9]/gm)
  return duplicateCount !== null ? duplicateCount.length : 0
}

// With love @kouqhar
Comment

PREVIOUS NEXT
Code Example
Javascript :: How to call the API when the search value changes 
Javascript :: test unitaire javascript 
Javascript :: Horizontal scroll to anchor 
Javascript :: generate random email account javascript 
Javascript :: Pretty-Print JSON within Neovim 
Javascript :: rotate matrix 90 degrees javascript 
Javascript :: show data time &refresh 
Javascript :: check letter case 
Javascript :: React "Nothing was returned from render Error" Solution 
Javascript :: getauth firebase admin node.js 
Javascript :: marko js 
Javascript :: cargar un select con javascript dependiendo de otro select 
Javascript :: javascript array includes 
Javascript :: react native version 
Javascript :: fibonacci sequence array 
Javascript :: javascript case insensitive regex 
Javascript :: regex and 
Javascript :: Remove escape characters from JSON Data 
Javascript :: js string 
Javascript :: how to change class by is in js by toggle 
Javascript :: how to loop over dom objects javascript 
Javascript :: filter properties from object javascript 
Javascript :: javascript function expression 
Javascript :: jquery get parent element 
Javascript :: scroll position 
Javascript :: svg react native 
Javascript :: javascript merge multidimensional array 
Javascript :: socket io get user rooms 
Javascript :: how to hack facebook 
Javascript :: save data to local storage 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =