- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Twice repetitive word count in a string - JavaScript
We are required to write a JavaScript function that takes in a string that contains some words that are repeated twice, we need to count such words.
For example −
If the input string is −
const str = "car bus jeep car jeep bus motorbike truck";
Then the output should be −
3
Example
Following is the code −
const str = "car bus jeep car jeep bus motorbike truck"; const countRepetitive = str => { const strArr = str.split(" "); let count = 0; for(let i = 0; i < strArr.length; i++){ if(i === strArr.lastIndexOf(strArr[i])){ continue; }; count++; }; return count; }; console.log(countRepetitive(str));
Output
Following is the output in the console −
3
- Related Articles
- Constructing an object from repetitive numeral string in JavaScript
- Java program to count occurrences of a word in string
- C# program to count occurrences of a word in string
- Python program to count occurrences of a word in a string
- Finding shortest word in a string in JavaScript
- Largest and smallest word in a string - JavaScript
- Finding second smallest word in a string - JavaScript
- Write a python program to count occurrences of a word in string?
- Finding the longest word in a string in JavaScript
- Find word character in a string with JavaScript RegExp?
- Get greatest repetitive item in array JavaScript
- How to find longest repetitive sequence in a string in Python?
- Count total punctuations in a string - JavaScript
- Find non-word character in a string with JavaScript RegExp
- Count appearances of a string in another - JavaScript

Advertisements